diff options
Diffstat (limited to 'src/com/android/camera/multi')
| -rw-r--r--[-rwxr-xr-x] | src/com/android/camera/multi/MultiCamera.java | 92 | ||||
| -rw-r--r-- | src/com/android/camera/multi/MultiCameraModule.java | 1329 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/com/android/camera/multi/MultiCameraUI.java | 1086 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/com/android/camera/multi/MultiCaptureModule.java | 1646 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/com/android/camera/multi/MultiSettingsActivity.java | 0 | ||||
| -rw-r--r-- | src/com/android/camera/multi/MultiVideoModule.java | 2670 |
6 files changed, 3429 insertions, 3394 deletions
diff --git a/src/com/android/camera/multi/MultiCamera.java b/src/com/android/camera/multi/MultiCamera.java index b63b4ab32..a934ec712 100755..100644 --- a/src/com/android/camera/multi/MultiCamera.java +++ b/src/com/android/camera/multi/MultiCamera.java @@ -1,47 +1,47 @@ -/*
-Copyright (c) 2020, The Linux Foundation. All rights reserved.
-
-Not a Contribution.
-
-Copyright (C) 2013 The Android Open Source Project
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-package com.android.camera.multi;
-
-public interface MultiCamera {
-
- boolean openCamera(String[] id);
-
- void startPreview();
-
- void closeSession();
-
- void closeCamera();
-
- void onShutterButtonClick(String[] ids);
-
- void onVideoButtonClick(String[] ids);
-
- void onPause();
-
- void onResume(String[] ids);
-
- void onButtonPause(String[] ids);
-
- void onButtonContinue(String[] ids);
-
- void onOrientationChanged(int orientation);
-
- boolean isRecordingVideo();
+/* +Copyright (c) 2020, The Linux Foundation. All rights reserved. + +Not a Contribution. + +Copyright (C) 2013 The Android Open Source Project + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package com.android.camera.multi; + +public interface MultiCamera { + + boolean openCamera(String[] id); + + void startPreview(); + + void closeSession(); + + void closeCamera(); + + void onShutterButtonClick(String[] ids); + + void onVideoButtonClick(String[] ids); + + void onPause(); + + void onResume(String[] ids); + + void onButtonPause(String[] ids); + + void onButtonContinue(String[] ids); + + void onOrientationChanged(int orientation); + + boolean isRecordingVideo(); }
\ No newline at end of file diff --git a/src/com/android/camera/multi/MultiCameraModule.java b/src/com/android/camera/multi/MultiCameraModule.java index 9f5745225..10ed17f93 100644 --- a/src/com/android/camera/multi/MultiCameraModule.java +++ b/src/com/android/camera/multi/MultiCameraModule.java @@ -1,655 +1,674 @@ -/*
- * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
- * Not a Contribution.
- *
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.camera.multi;
-
-import android.content.Context;
-import android.content.Intent;
-import android.content.res.Configuration;
-import android.graphics.Rect;
-import android.hardware.camera2.CameraManager;
-import android.hardware.camera2.CameraAccessException;
-import android.hardware.camera2.CameraCharacteristics;
-import android.media.AudioManager;
-import android.os.Handler;
-import android.os.HandlerThread;
-import android.os.Looper;
-import android.os.Message;
-import android.view.WindowManager;
-import android.view.View;
-import android.view.KeyEvent;
-import android.view.OrientationEventListener;
-import android.util.Log;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Set;
-
-import com.android.camera.CameraModule;
-import com.android.camera.CameraActivity;
-import com.android.camera.MediaSaveService;
-import com.android.camera.PhotoController;
-import com.android.camera.util.CameraUtil;
-import com.android.camera.data.Camera2ModeAdapter.OnItemClickListener;
-
-public class MultiCameraModule implements CameraModule, PhotoController {
-
- private static final String TAG = "SnapCam_MultiCameraModule";
- private static final int MAX_NUM_CAM = 16;
-
- private static final int OPEN_CAMERA = 0;
-
- public static final int CLEAR_SCREEN_DELAY = 101;
-
- private View mRootView;
- private MultiCameraUI mUI;
- private CameraActivity mActivity;
- private MultiCamera mMultiCamera;
-
- /**
- * An additional thread for running tasks that shouldn't block the UI.
- */
- private HandlerThread mCameraThread;
- private HandlerThread mCaptureCallbackThread;
-
- private Handler mCameraHandler;
- private Handler mCaptureCallbackHandler;
-
- private final Handler mHandler = new MainHandler();
-
- private int mDisplayRotation;
- private int mDisplayOrientation;
-
- private boolean mCameraModeSwitcherAllowed = true;
- private int mNextModeIndex = 1;
- private int mCurrentModeIndex = 1;
-
- // The degrees of the device rotated clockwise from its natural orientation.
- private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
-
- private boolean mIsMute = false;
-
- private boolean[] mTakingPicture = new boolean[MAX_NUM_CAM];
-
- private ArrayList<SceneModule> mSceneCameraIds = new ArrayList<>();
- private String[] mSelectableModes = {"Video", "Photo"};
- private SceneModule mCurrentSceneMode;
- private String[] mCameraIds = new String[2];
-
- public static int CURRENT_ID = 0;
- public static CameraMode CURRENT_MODE = CameraMode.DEFAULT;
-
- public enum CameraMode {
- VIDEO,
- DEFAULT
- }
-
- public Handler getMainHandler() {
- return mHandler;
- }
-
- public Handler getMyCameraHandler() {
- return mCameraHandler;
- }
-
- public void reinit() {
- }
-
- public List<String> getCameraModeList() {
- ArrayList<String> cameraModes = new ArrayList<>();
- for (SceneModule sceneModule : mSceneCameraIds) {
- cameraModes.add(mSelectableModes[sceneModule.mode.ordinal()]);
- }
- return cameraModes;
- }
-
- public OnItemClickListener getModeItemClickListener() {
- return new OnItemClickListener() {
- @Override
- public int onItemClick(int mode) {
- Log.v(TAG, " onItemClick mode :" + mode);
- if (!getCameraModeSwitcherAllowed()) {
- return -1;
- }
- return selectCameraMode(mode);
- }
- };
- }
-
- public int selectCameraMode(int mode) {
- if (mCurrentSceneMode.mode == mSceneCameraIds.get(mode).mode) {
- return -1;
- }
- setCameraModeSwitcherAllowed(true);
- setNextSceneMode(mode);
- mCurrentSceneMode = mSceneCameraIds.get(mode);
- SceneModule nextSceneMode = mSceneCameraIds.get(mode);
- restartAll();
- return 1;
- }
-
- public void setNextSceneMode(int index) {
- mNextModeIndex = index;
- }
-
- public void setCameraModeSwitcherAllowed(boolean allow) {
- mCameraModeSwitcherAllowed = allow;
- }
-
- public int getCurrentModeIndex() {
- return mCurrentModeIndex;
- }
-
- public void setCurrentSceneModeOnly(int mode) {
- mCurrentSceneMode = mSceneCameraIds.get(mode);
- mCurrentModeIndex = mode;
- CURRENT_ID = mCurrentSceneMode.getCurrentId();
- CURRENT_MODE = mCurrentSceneMode.mode;
- }
-
- public void restartAll() {
- Log.d(TAG, "restart all");
- onPauseBeforeSuper();
- onPauseAfterSuper();
- setCurrentSceneModeOnly(mNextModeIndex);
- updateMultiCamera();
- onResumeBeforeSuper();
- onResumeAfterSuper();
- }
-
- private void updateMultiCamera() {
- mMultiCamera = null;
- if (mCurrentSceneMode.mode == CameraMode.VIDEO) {
- mMultiCamera = new MultiVideoModule(mActivity, mUI, this);
- } else if (mCurrentSceneMode.mode == CameraMode.DEFAULT) {
- mMultiCamera = new MultiCaptureModule(mActivity, mUI, this);
- }
- }
-
- public boolean getCameraModeSwitcherAllowed() {
- return mCameraModeSwitcherAllowed;
- }
-
- public void onVideoButtonClick() {
- Log.d(TAG, "onVideoButtonClick");
- mMultiCamera.onVideoButtonClick(mCameraIds);
- }
-
- public boolean isTakingPicture() {
- for (int i = 0; i < mTakingPicture.length; i++) {
- if (mTakingPicture[i]) return true;
- }
- return false;
- }
-
- public boolean isRecordingVideo() {
- return mMultiCamera.isRecordingVideo();
- }
-
- public void updateTakingPicture() {
- for (int i = 0; i < mTakingPicture.length; i++) {
- mTakingPicture[i] = false;
- }
- }
-
- public void setMute(boolean enable, boolean isValue) {
- AudioManager am = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
- am.setMicrophoneMute(enable);
- if (isValue) {
- mIsMute = enable;
- }
- }
-
- public boolean isAudioMute() {
- return mIsMute;
- }
-
- public void onButtonPause() {
- Log.v(TAG, "onButtonPause");
- mMultiCamera.onButtonPause(mCameraIds);
- }
-
- public void onButtonContinue() {
- Log.v(TAG, "onButtonContinue");
- mMultiCamera.onButtonContinue(mCameraIds);
- }
-
- public int[] getOpenCameraIdList() {
- int[] result = new int[2];
- result[0] = 0;
- result[1] = 2;
- return result;
- }
-
- public CameraMode getCurrenCameraMode() {
- if (mCurrentSceneMode == null) {
- Log.w(TAG, "getCurrenCameraMode mCurrentSceneMode is NULL retrun CameraMode.DEFAULT");
- return CameraMode.DEFAULT;
- } else {
- return mCurrentSceneMode.mode;
- }
- }
-
- @Override
- public void init(CameraActivity activity, View parent) {
- SceneModule module;
- Log.v(TAG, " init ");
- for (int i = 0; i < mSelectableModes.length; i++) {
- module = new SceneModule();
- module.mode = CameraMode.values()[i];
- mSceneCameraIds.add(module);
- }
-
- for (int i = 0; i < MAX_NUM_CAM; i++) {
- mTakingPicture[i] = false;
- }
-
- mCurrentSceneMode = mSceneCameraIds.get(1);
-
- mActivity = activity;
- mRootView = parent;
- if (mUI == null) {
- mUI = new MultiCameraUI(activity, this, parent);
- }
- mMultiCamera = new MultiCaptureModule(mActivity, mUI, this);
- startBackgroundThread();
- initCameraIds();
- }
-
- @Override
- public void onPreviewFocusChanged(boolean previewFocused) {
-
- }
-
- @Override
- public void onResumeBeforeSuper() {
- for (int i = 0; i < MAX_NUM_CAM; i++) {
- mTakingPicture[i] = false;
- }
- startBackgroundThread();
- }
-
- @Override
- public void onResumeAfterSuper() {
- Log.d(TAG, " onResumeAfterSuper ");
- mCameraIds[0] = "0";
- mCameraIds[1] = "2";
- mMultiCamera.onResume(mCameraIds);
- Message msg = Message.obtain();
- msg.what = OPEN_CAMERA;
- if (mCameraHandler != null) {
- mCameraHandler.sendMessage(msg);
- }
- mUI.showRelatedIcons(mCurrentSceneMode.mode);
- }
-
- @Override
- public void onPauseBeforeSuper() {
- Log.d(TAG, " onPauseBeforeSuper ");
- mMultiCamera.onPause();
- }
-
- @Override
- public void onPauseAfterSuper() {
- stopBackgroundThread();
- }
-
- @Override
- public void onConfigurationChanged(Configuration config) {
-
- }
-
- @Override
- public void onStop() {
-
- }
-
- @Override
- public void onDestroy() {
-
- }
-
- @Override
- public void installIntentFilter() {
-
- }
-
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
-
- }
-
- @Override
- public boolean onBackPressed() {
- return false;
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- return false;
- }
-
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- return false;
- }
-
- @Override
- public void onSingleTapUp(View view, int x, int y) {
-
- }
-
- @Override
- public void onLongPress(View view, int x, int y) {
- }
-
- @Override
- public void onPreviewTextureCopied() {
-
- }
-
- @Override
- public void onCaptureTextureCopied() {
-
- }
-
- @Override
- public void onUserInteraction() {
-
- }
-
- @Override
- public boolean updateStorageHintOnResume() {
- return false;
- }
-
- @Override
- public void onOrientationChanged(int orientation) {
- // We keep the last known orientation. So if the user first orient
- // the camera then point the camera to floor or sky, we still have
- // the correct orientation.
- if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return;
- int oldOrientation = mOrientation;
- mOrientation = CameraUtil.roundOrientation(orientation, mOrientation);
- if (oldOrientation != mOrientation) {
- mUI.setOrientation(mOrientation, true);
- mMultiCamera.onOrientationChanged(mOrientation);
- }
-
- }
-
- @Override
- public void onShowSwitcherPopup() {
-
- }
-
- @Override
- public void onMediaSaveServiceConnected(MediaSaveService s) {
-
- }
-
- @Override
- public boolean arePreviewControlsVisible() {
- return false;
- }
-
- @Override
- public void resizeForPreviewAspectRatio() {
-
- }
-
- @Override
- public void waitingLocationPermissionResult(boolean waiting) {
-
- }
-
- @Override
- public void enableRecordingLocation(boolean enable) {
-
- }
-
- @Override
- public void setPreferenceForTest(String key, String value) {
-
- }
-
- @Override
- public int onZoomChanged(int requestedZoom) {
- return 0;
- }
-
- @Override
- public void onZoomChanged(float requestedZoom) {
- }
-
- @Override
- public boolean isImageCaptureIntent() {
- return false;
- }
-
- @Override
- public boolean isCameraIdle() {
- return true;
- }
-
- @Override
- public void onCaptureDone() {
-
- }
-
- @Override
- public void onCaptureCancelled() {
-
- }
-
- @Override
- public void onCaptureRetake() {
-
- }
-
- @Override
- public void cancelAutoFocus() {
-
- }
-
- @Override
- public void stopPreview() {
-
- }
-
- @Override
- public int getCameraState() {
- return 0;
- }
-
- @Override
- public void onCountDownFinished() {
- }
-
- @Override
- public void onScreenSizeChanged(int width, int height) {
-
- }
-
- @Override
- public void onPreviewRectChanged(Rect previewRect) {
-
- }
-
- @Override
- public void updateCameraOrientation() {
- if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) {
- setDisplayOrientation();
- }
- }
-
- @Override
- public void onPreviewUIReady() {
- }
-
- @Override
- public void onPreviewUIDestroyed() {
- }
-
- @Override
- public void onShutterButtonClick() {
- Log.d(TAG, "onShutterButtonClick");
- mMultiCamera.onShutterButtonClick(mCameraIds);
- for (String CameraId : mCameraIds) {
- int id = Integer.parseInt(CameraId);
- mTakingPicture[id] = true;
- }
- }
-
- @Override
- public void onShutterButtonLongClick() {
-
- }
-
- @Override
- public void onShutterButtonFocus(boolean pressed) {
-
- }
-
- private class SceneModule {
- CameraMode mode = CameraMode.DEFAULT;
- public int rearCameraId;
- public int frontCameraId;
- public int auxCameraId = 0;
- int getCurrentId() {
- return rearCameraId;
- }
- }
-
- /**
- * This Handler is used to post message back onto the main thread of the
- * application
- */
- private class MainHandler extends Handler {
- public MainHandler() {
- super(Looper.getMainLooper());
- }
-
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case CLEAR_SCREEN_DELAY: {
- mActivity.getWindow().clearFlags(
- WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- break;
- }
- }
- }
- }
-
- private void initCameraIds() {
- CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
- boolean isFirstDefault = true;
- String[] cameraIdList = null;
- try {
- cameraIdList = manager.getCameraIdList();
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- if (cameraIdList == null || cameraIdList.length == 0) {
- return;
- }
- for (int i = 0; i < cameraIdList.length; i++) {
- String cameraId = cameraIdList[i];
- CameraCharacteristics characteristics;
- try {
- characteristics = manager.getCameraCharacteristics(cameraId);
- } catch (CameraAccessException e) {
- e.printStackTrace();
- continue;
- }
- Set<String> physicalCameraIds = characteristics.getPhysicalCameraIds();
- for (String camId : physicalCameraIds) {
- Log.v(TAG, "initCameraIds physicalCameraIds :" + physicalCameraIds);
- Log.v(TAG, "initCameraIds >>>> camId :" + camId + " cameraId :" + cameraId + ", i :" + i);
- }
- }
- }
-
- /**
- * Starts a background thread and its {@link Handler}.
- */
- private void startBackgroundThread() {
- if (mCameraThread == null) {
- mCameraThread = new HandlerThread("CameraBackground");
- mCameraThread.start();
- }
- if (mCaptureCallbackThread == null) {
- mCaptureCallbackThread = new HandlerThread("CameraCaptureCallback");
- mCaptureCallbackThread.start();
- }
-
- if (mCameraHandler == null) {
- mCameraHandler = new MyCameraHandler(mCameraThread.getLooper());
- }
- if (mCaptureCallbackHandler == null) {
- mCaptureCallbackHandler = new Handler(mCaptureCallbackThread.getLooper());
- }
- }
-
- private void stopBackgroundThread() {
- mCameraThread.quitSafely();
- try {
- mCameraThread.join();
- mCameraThread = null;
- mCameraHandler = null;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- mCaptureCallbackThread.quitSafely();
- try {
- mCaptureCallbackThread.join();
- mCaptureCallbackThread = null;
- mCaptureCallbackHandler = null;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- private class MyCameraHandler extends Handler {
-
- public MyCameraHandler(Looper looper) {
- super(looper);
- }
-
- @Override
- public void handleMessage(Message msg) {
- int id = msg.arg1;
- switch (msg.what) {
- case OPEN_CAMERA:
- if (mMultiCamera != null) {
- mMultiCamera.openCamera(mCameraIds);
- }
- break;
- }
- }
- }
-
- private void setDisplayOrientation() {
- mDisplayRotation = CameraUtil.getDisplayRotation(mActivity);
- mDisplayOrientation = CameraUtil.getDisplayOrientationForCamera2(
- mDisplayRotation, 0);
- }
-
-}
+/* + * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.camera.multi; + +import android.content.Context; +import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.TypedArray; +import android.graphics.Rect; +import android.hardware.camera2.CameraManager; +import android.hardware.camera2.CameraAccessException; +import android.hardware.camera2.CameraCharacteristics; +import android.media.AudioManager; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Looper; +import android.os.Message; +import android.view.WindowManager; +import android.view.View; +import android.view.KeyEvent; +import android.view.OrientationEventListener; +import android.util.Log; + +import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Set; + +import com.android.camera.CameraModule; +import com.android.camera.CameraActivity; +import com.android.camera.MediaSaveService; +import com.android.camera.PhotoController; +import com.android.camera.util.CameraUtil; +import com.android.camera.data.Camera2ModeAdapter.OnItemClickListener; + +import org.codeaurora.snapcam.R; + +public class MultiCameraModule implements CameraModule, PhotoController { + + private static final String TAG = "SnapCam_MultiCameraModule"; + private static final int MAX_NUM_CAM = 16; + + private static final int OPEN_CAMERA = 0; + + public static final int CLEAR_SCREEN_DELAY = 101; + + private View mRootView; + private MultiCameraUI mUI; + private CameraActivity mActivity; + private MultiCamera mMultiCamera; + + /** + * An additional thread for running tasks that shouldn't block the UI. + */ + private HandlerThread mCameraThread; + private HandlerThread mCaptureCallbackThread; + + private Handler mCameraHandler; + private Handler mCaptureCallbackHandler; + + private final Handler mHandler = new MainHandler(); + + private int mDisplayRotation; + private int mDisplayOrientation; + + private boolean mCameraModeSwitcherAllowed = true; + private int mNextModeIndex = 1; + private int mCurrentModeIndex = 1; + + // The degrees of the device rotated clockwise from its natural orientation. + private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; + + private boolean mIsMute = false; + + private boolean[] mTakingPicture = new boolean[MAX_NUM_CAM]; + + private ArrayList<SceneModule> mSceneCameraIds = new ArrayList<>(); + private String[] mSelectableModes = {"Video", "Photo"}; + private SceneModule mCurrentSceneMode; + private String[] mCameraIds = new String[2]; + + public static int CURRENT_ID = 0; + public static CameraMode CURRENT_MODE = CameraMode.DEFAULT; + + public enum CameraMode { + VIDEO, + DEFAULT + } + + public Handler getMainHandler() { + return mHandler; + } + + public Handler getMyCameraHandler() { + return mCameraHandler; + } + + public void reinit() { + } + + public List<String> getCameraModeList() { + ArrayList<String> cameraModes = new ArrayList<>(); + for (SceneModule sceneModule : mSceneCameraIds) { + cameraModes.add(mSelectableModes[sceneModule.mode.ordinal()]); + } + return cameraModes; + } + + public OnItemClickListener getModeItemClickListener() { + return new OnItemClickListener() { + @Override + public int onItemClick(int mode) { + Log.v(TAG, " onItemClick mode :" + mode); + if (!getCameraModeSwitcherAllowed()) { + return -1; + } + return selectCameraMode(mode); + } + }; + } + + public int selectCameraMode(int mode) { + if (mCurrentSceneMode.mode == mSceneCameraIds.get(mode).mode) { + return -1; + } + setCameraModeSwitcherAllowed(true); + setNextSceneMode(mode); + mCurrentSceneMode = mSceneCameraIds.get(mode); + SceneModule nextSceneMode = mSceneCameraIds.get(mode); + restartAll(); + return 1; + } + + public void setNextSceneMode(int index) { + mNextModeIndex = index; + } + + public void setCameraModeSwitcherAllowed(boolean allow) { + mCameraModeSwitcherAllowed = allow; + } + + public int getCurrentModeIndex() { + return mCurrentModeIndex; + } + + public List<Integer> getCameraModeIconList() { + ArrayList<Integer> cameraModeIcons = new ArrayList<>(); + TypedArray ic = mActivity.getResources() + .obtainTypedArray(R.array.camera_modes_front); + for (SceneModule sceneModule : mSceneCameraIds) { + cameraModeIcons.add(ic.getResourceId(sceneModule.mode.ordinal(), 0)); + } + return cameraModeIcons; + } + + public void setCurrentSceneModeOnly(int mode) { + mCurrentSceneMode = mSceneCameraIds.get(mode); + mCurrentModeIndex = mode; + CURRENT_ID = mCurrentSceneMode.getCurrentId(); + CURRENT_MODE = mCurrentSceneMode.mode; + } + + public void restartAll() { + Log.d(TAG, "restart all"); + onPauseBeforeSuper(); + onPauseAfterSuper(); + setCurrentSceneModeOnly(mNextModeIndex); + updateMultiCamera(); + onResumeBeforeSuper(); + onResumeAfterSuper(); + } + + private void updateMultiCamera() { + mMultiCamera = null; + if (mCurrentSceneMode.mode == CameraMode.VIDEO) { + mMultiCamera = new MultiVideoModule(mActivity, mUI, this); + } else if (mCurrentSceneMode.mode == CameraMode.DEFAULT) { + mMultiCamera = new MultiCaptureModule(mActivity, mUI, this); + } + } + + public boolean getCameraModeSwitcherAllowed() { + return mCameraModeSwitcherAllowed; + } + + public void onVideoButtonClick() { + Log.d(TAG, "onVideoButtonClick"); + mMultiCamera.onVideoButtonClick(mCameraIds); + } + + public boolean isTakingPicture() { + for (int i = 0; i < mTakingPicture.length; i++) { + if (mTakingPicture[i]) return true; + } + return false; + } + + public boolean isRecordingVideo() { + return mMultiCamera.isRecordingVideo(); + } + + public void updateTakingPicture() { + for (int i = 0; i < mTakingPicture.length; i++) { + mTakingPicture[i] = false; + } + } + + public void setMute(boolean enable, boolean isValue) { + AudioManager am = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE); + am.setMicrophoneMute(enable); + if (isValue) { + mIsMute = enable; + } + } + + public boolean isAudioMute() { + return mIsMute; + } + + public void onButtonPause() { + Log.v(TAG, "onButtonPause"); + mMultiCamera.onButtonPause(mCameraIds); + } + + public void onButtonContinue() { + Log.v(TAG, "onButtonContinue"); + mMultiCamera.onButtonContinue(mCameraIds); + } + + public int[] getOpenCameraIdList() { + int[] result = new int[2]; + result[0] = 0; + result[1] = 2; + return result; + } + + public CameraMode getCurrenCameraMode() { + if (mCurrentSceneMode == null) { + Log.w(TAG, "getCurrenCameraMode mCurrentSceneMode is NULL retrun CameraMode.DEFAULT"); + return CameraMode.DEFAULT; + } else { + return mCurrentSceneMode.mode; + } + } + + @Override + public void init(CameraActivity activity, View parent) { + SceneModule module; + Log.v(TAG, " init "); + for (int i = 0; i < mSelectableModes.length; i++) { + module = new SceneModule(); + module.mode = CameraMode.values()[i]; + mSceneCameraIds.add(module); + } + + for (int i = 0; i < MAX_NUM_CAM; i++) { + mTakingPicture[i] = false; + } + + mCurrentSceneMode = mSceneCameraIds.get(1); + + mActivity = activity; + mRootView = parent; + if (mUI == null) { + mUI = new MultiCameraUI(activity, this, parent); + } + mMultiCamera = new MultiCaptureModule(mActivity, mUI, this); + startBackgroundThread(); + initCameraIds(); + } + + @Override + public void onPreviewFocusChanged(boolean previewFocused) { + + } + + @Override + public void onResumeBeforeSuper() { + for (int i = 0; i < MAX_NUM_CAM; i++) { + mTakingPicture[i] = false; + } + startBackgroundThread(); + } + + @Override + public void onResumeAfterSuper() { + Log.d(TAG, " onResumeAfterSuper "); + mCameraIds[0] = "0"; + mCameraIds[1] = "2"; + mMultiCamera.onResume(mCameraIds); + Message msg = Message.obtain(); + msg.what = OPEN_CAMERA; + if (mCameraHandler != null) { + mCameraHandler.sendMessage(msg); + } + mUI.showRelatedIcons(mCurrentSceneMode.mode); + } + + @Override + public void onPauseBeforeSuper() { + Log.d(TAG, " onPauseBeforeSuper "); + mMultiCamera.onPause(); + } + + @Override + public void onPauseAfterSuper() { + stopBackgroundThread(); + } + + @Override + public void onConfigurationChanged(Configuration config) { + + } + + @Override + public void onStop() { + + } + + @Override + public void onDestroy() { + + } + + @Override + public void installIntentFilter() { + + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + + } + + @Override + public boolean onBackPressed() { + return false; + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return false; + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + return false; + } + + @Override + public void onSingleTapUp(View view, int x, int y) { + + } + + @Override + public void onLongPress(View view, int x, int y) { + } + + @Override + public void onPreviewTextureCopied() { + + } + + @Override + public void onCaptureTextureCopied() { + + } + + @Override + public void onUserInteraction() { + + } + + @Override + public boolean updateStorageHintOnResume() { + return false; + } + + @Override + public void onOrientationChanged(int orientation) { + // We keep the last known orientation. So if the user first orient + // the camera then point the camera to floor or sky, we still have + // the correct orientation. + if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return; + int oldOrientation = mOrientation; + mOrientation = CameraUtil.roundOrientation(orientation, mOrientation); + if (oldOrientation != mOrientation) { + mUI.setOrientation(mOrientation, true); + mMultiCamera.onOrientationChanged(mOrientation); + } + + } + + @Override + public void onShowSwitcherPopup() { + + } + + @Override + public void onMediaSaveServiceConnected(MediaSaveService s) { + + } + + @Override + public boolean arePreviewControlsVisible() { + return false; + } + + @Override + public void resizeForPreviewAspectRatio() { + + } + + @Override + public void onSwitchSavePath() { + + } + + @Override + public void waitingLocationPermissionResult(boolean waiting) { + + } + + @Override + public void enableRecordingLocation(boolean enable) { + + } + + @Override + public void setPreferenceForTest(String key, String value) { + + } + + @Override + public int onZoomChanged(int requestedZoom) { + return 0; + } + + @Override + public void onZoomChanged(float requestedZoom) { + } + + @Override + public boolean isImageCaptureIntent() { + return false; + } + + @Override + public boolean isCameraIdle() { + return true; + } + + @Override + public void onCaptureDone() { + + } + + @Override + public void onCaptureCancelled() { + + } + + @Override + public void onCaptureRetake() { + + } + + @Override + public void cancelAutoFocus() { + + } + + @Override + public void stopPreview() { + + } + + @Override + public int getCameraState() { + return 0; + } + + @Override + public void onCountDownFinished() { + } + + @Override + public void onScreenSizeChanged(int width, int height) { + + } + + @Override + public void onPreviewRectChanged(Rect previewRect) { + + } + + @Override + public void updateCameraOrientation() { + if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) { + setDisplayOrientation(); + } + } + + @Override + public void onPreviewUIReady() { + } + + @Override + public void onPreviewUIDestroyed() { + } + + @Override + public void onShutterButtonClick() { + Log.d(TAG, "onShutterButtonClick"); + mMultiCamera.onShutterButtonClick(mCameraIds); + for (String CameraId : mCameraIds) { + int id = Integer.parseInt(CameraId); + mTakingPicture[id] = true; + } + } + + @Override + public void onShutterButtonLongClick() { + + } + + @Override + public void onShutterButtonFocus(boolean pressed) { + + } + + private class SceneModule { + CameraMode mode = CameraMode.DEFAULT; + public int rearCameraId; + public int frontCameraId; + public int auxCameraId = 0; + int getCurrentId() { + return rearCameraId; + } + } + + /** + * This Handler is used to post message back onto the main thread of the + * application + */ + private class MainHandler extends Handler { + public MainHandler() { + super(Looper.getMainLooper()); + } + + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case CLEAR_SCREEN_DELAY: { + mActivity.getWindow().clearFlags( + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + break; + } + } + } + } + + private void initCameraIds() { + CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE); + boolean isFirstDefault = true; + String[] cameraIdList = null; + try { + cameraIdList = manager.getCameraIdList(); + } catch (CameraAccessException e) { + e.printStackTrace(); + } + if (cameraIdList == null || cameraIdList.length == 0) { + return; + } + for (int i = 0; i < cameraIdList.length; i++) { + String cameraId = cameraIdList[i]; + CameraCharacteristics characteristics; + try { + characteristics = manager.getCameraCharacteristics(cameraId); + } catch (CameraAccessException e) { + e.printStackTrace(); + continue; + } + Set<String> physicalCameraIds = characteristics.getPhysicalCameraIds(); + for (String camId : physicalCameraIds) { + Log.v(TAG, "initCameraIds physicalCameraIds :" + physicalCameraIds); + Log.v(TAG, "initCameraIds >>>> camId :" + camId + " cameraId :" + cameraId + ", i :" + i); + } + } + } + + /** + * Starts a background thread and its {@link Handler}. + */ + private void startBackgroundThread() { + if (mCameraThread == null) { + mCameraThread = new HandlerThread("CameraBackground"); + mCameraThread.start(); + } + if (mCaptureCallbackThread == null) { + mCaptureCallbackThread = new HandlerThread("CameraCaptureCallback"); + mCaptureCallbackThread.start(); + } + + if (mCameraHandler == null) { + mCameraHandler = new MyCameraHandler(mCameraThread.getLooper()); + } + if (mCaptureCallbackHandler == null) { + mCaptureCallbackHandler = new Handler(mCaptureCallbackThread.getLooper()); + } + } + + private void stopBackgroundThread() { + mCameraThread.quitSafely(); + try { + mCameraThread.join(); + mCameraThread = null; + mCameraHandler = null; + } catch (InterruptedException e) { + e.printStackTrace(); + } + + mCaptureCallbackThread.quitSafely(); + try { + mCaptureCallbackThread.join(); + mCaptureCallbackThread = null; + mCaptureCallbackHandler = null; + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + private class MyCameraHandler extends Handler { + + public MyCameraHandler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + int id = msg.arg1; + switch (msg.what) { + case OPEN_CAMERA: + if (mMultiCamera != null) { + mMultiCamera.openCamera(mCameraIds); + } + break; + } + } + } + + private void setDisplayOrientation() { + mDisplayRotation = CameraUtil.getDisplayRotation(mActivity); + mDisplayOrientation = CameraUtil.getDisplayOrientationForCamera2( + mDisplayRotation, 0); + } + +} diff --git a/src/com/android/camera/multi/MultiCameraUI.java b/src/com/android/camera/multi/MultiCameraUI.java index df4c54341..8391f8bae 100755..100644 --- a/src/com/android/camera/multi/MultiCameraUI.java +++ b/src/com/android/camera/multi/MultiCameraUI.java @@ -1,539 +1,549 @@ -/*
- * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
- * Not a Contribution.
- *
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.camera.multi;
-
-import android.content.Intent;
-import android.graphics.Bitmap;
-import android.graphics.drawable.AnimationDrawable;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.ImageView;
-import android.widget.TextView;
-import android.view.SurfaceHolder;
-import android.util.Log;
-
-import android.support.v7.widget.LinearLayoutManager;
-import android.support.v7.widget.RecyclerView;
-
-import com.android.camera.CameraActivity;
-import com.android.camera.PauseButton;
-import com.android.camera.PreviewGestures;
-import com.android.camera.data.Camera2ModeAdapter;
-import com.android.camera.ShutterButton;
-import com.android.camera.ui.AutoFitSurfaceView;
-import com.android.camera.ui.CameraControls;
-import com.android.camera.ui.Camera2FaceView;
-import com.android.camera.ui.CountDownView;
-import com.android.camera.ui.FlashToggleButton;
-import com.android.camera.ui.OneUICameraControls;
-import com.android.camera.ui.RenderOverlay;
-import com.android.camera.ui.RotateImageView;
-import com.android.camera.ui.RotateLayout;
-
-import org.codeaurora.snapcam.R;
-
-import java.util.ArrayList;
-
-public class MultiCameraUI implements PreviewGestures.SingleTapListener,
- PauseButton.OnPauseButtonListener {
-
- private static final String TAG = "SnapCam_MultiCameraUI";
-
- private static final int MAX_NUM_CAM = 16;
-
- private static final int PREVIEW_WIDTH = 1024;
- private static final int PREVIEW_HIEGHT = 768;
-
- private CameraActivity mActivity;
- private View mRootView;
- private MultiCameraModule mModule;
- private PreviewGestures mGestures;
-
- /**
- * An {@link AutoFitTextureView} for camera preview.
- */
- private AutoFitSurfaceView mMainPreviewSurface;
- private AutoFitSurfaceView mFirstPreviewSurface;
- private AutoFitSurfaceView mSecondPreviewSurface;
- private AutoFitSurfaceView mPreviewSurface;
- private ArrayList<AutoFitSurfaceView> mSurfaceViewList = new ArrayList();
-
- private SurfaceHolder mMainSurfaceHolder;
- private SurfaceHolder mFirstSurfaceHolder;
- private SurfaceHolder mSecondSurfaceHolder;
-
- private RenderOverlay mRenderOverlay;
- private ShutterButton mShutterButton;
- private PauseButton mPauseButton;
- private RotateImageView mMuteButton;
- private ImageView mVideoButton;
- private ImageView mThumbnail;
- private ImageView mSettingsIcon;
-
- private CountDownView mCountDownView;
- private OneUICameraControls mCameraControls;
- private Camera2FaceView mFaceView;
-
- private TextView mRecordingTimeView;
- private View mTimeLapseLabel;
- private RotateLayout mRecordingTimeRect;
-
- private FlashToggleButton mFlashButton;
- private View mFilterModeSwitcher;
- private View mSceneModeSwitcher;
-
- private int mOrientation;
-
- private RecyclerView mModeSelectLayout;
- private Camera2ModeAdapter mCameraModeAdapter;
-
- private int mPreviewWidths[] = new int[MAX_NUM_CAM];
- private int mPreviewHeights[] = new int[MAX_NUM_CAM];
-
- public MultiCameraUI(CameraActivity activity, final MultiCameraModule module, View parent) {
- mActivity = activity;
- mModule = module;
- mRootView = parent;
- mActivity.getLayoutInflater().inflate(R.layout.multi_camera_module,
- (ViewGroup) mRootView, true);
-
- initPreviewSurface();
- initCameraControls();
- initShutterButton();
- initVideoButton();
- initVideoMuteButton();
- initializeThumbnail();
- hideMenuButton();
- initPauseButton();
- initSettingsMenu();
- initModeSelectLayout();
- }
-
- @Override
- public void onButtonPause() {
- mRecordingTimeView.setCompoundDrawablesWithIntrinsicBounds(
- R.drawable.ic_pausing_indicator, 0, 0, 0);
- mModule.onButtonPause();
- }
-
- @Override
- public void onButtonContinue() {
- mRecordingTimeView.setCompoundDrawablesWithIntrinsicBounds(
- R.drawable.ic_recording_indicator, 0, 0, 0);
- mModule.onButtonContinue();
- }
-
- public void showSurfaceView(int index) {
- Log.d(TAG, "showSurfaceView" + mPreviewWidths[index] + " " + mPreviewHeights[index]);
- mSurfaceViewList.get(index).getHolder().setFixedSize(mPreviewWidths[index], mPreviewHeights[index]);
- mSurfaceViewList.get(index).setAspectRatio(mPreviewHeights[index], mPreviewWidths[index]);
- mSurfaceViewList.get(index).setVisibility(View.VISIBLE);
- }
-
- public boolean setPreviewSize(int index, int width, int height) {
- Log.d(TAG, "setPreviewSize " + width + " " + height);
- boolean changed = (width != mPreviewWidths[index]) || (height != mPreviewHeights[index]);
- mPreviewWidths[index] = width;
- mPreviewHeights[index] = height;
- if (changed) {
- showSurfaceView(index);
- }
- return changed;
- }
-
- private void initPreviewSurface() {
- // Multi camera preview
- mMainPreviewSurface = (AutoFitSurfaceView) mRootView.findViewById(R.id.main_preview_content);
- mFirstPreviewSurface = (AutoFitSurfaceView) mRootView.findViewById(R.id.first_preview_content);
- mSecondPreviewSurface = (AutoFitSurfaceView) mRootView.findViewById(R.id.second_preview_content);
-
- mSurfaceViewList.add(mMainPreviewSurface);
- mSurfaceViewList.add(mFirstPreviewSurface);
- mSurfaceViewList.add(mSecondPreviewSurface);
-
- mMainSurfaceHolder = mMainPreviewSurface.getHolder();
- mMainSurfaceHolder.addCallback(mMainSurfaceHolderCallback);
- mFirstSurfaceHolder = mFirstPreviewSurface.getHolder();
- mFirstSurfaceHolder.addCallback(mFirstHolderCallback);
- mSecondSurfaceHolder = mSecondPreviewSurface.getHolder();
- mSecondSurfaceHolder.addCallback(mSecondHolderCallback);
-
- mMainPreviewSurface.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
- @Override
- public void onLayoutChange(View v, int left, int top, int right,
- int bottom, int oldLeft, int oldTop, int oldRight,
- int oldBottom) {
- int width = right - left;
- int height = bottom - top;
- }
- });
- }
-
- private void initShutterButton() {
- mShutterButton = (ShutterButton) mRootView.findViewById(R.id.shutter_button);
- mShutterButton.setOnShutterButtonListener(mModule);
- mShutterButton.setImageResource(R.drawable.btn_new_shutter);
- }
-
- private void initializeThumbnail() {
- if (mThumbnail == null) {
- mThumbnail = (ImageView) mRootView.findViewById(R.id.preview_thumb);
- }
- mActivity.updateThumbnail(mThumbnail);
- mThumbnail.setVisibility(View.INVISIBLE);
- mThumbnail.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (!CameraControls.isAnimating() && !mModule.isTakingPicture() &&
- !mModule.isRecordingVideo()) {
- mActivity.gotoGallery();
- }
- }
- });
- }
-
- private void initModeSelectLayout() {
- mRenderOverlay = (RenderOverlay) mRootView.findViewById(R.id.render_overlay);
- mModeSelectLayout = (RecyclerView) mRootView.findViewById(R.id.mode_select_layout);
- mModeSelectLayout.setLayoutManager(new LinearLayoutManager(mActivity,
- LinearLayoutManager.HORIZONTAL, false));
- mCameraModeAdapter = new Camera2ModeAdapter(mModule.getCameraModeList());
- mCameraModeAdapter.setSelectedPosition(1);
- mCameraModeAdapter.setOnItemClickListener(mModule.getModeItemClickListener());
- mModeSelectLayout.setAdapter(mCameraModeAdapter);
- mModeSelectLayout.setVisibility(View.VISIBLE);
-
- if (mGestures == null) {
- // this will handle gesture disambiguation and dispatching
- mGestures = new PreviewGestures(mActivity, this, null, null, null);
- mRenderOverlay.setGestures(mGestures);
- }
-
- mGestures.setRenderOverlay(mRenderOverlay);
- mRenderOverlay.requestLayout();
- mActivity.setPreviewGestures(mGestures);
- }
-
- private void initCameraControls() {
- mCameraControls = (OneUICameraControls) mRootView.findViewById(R.id.camera_controls);
- mFaceView = (Camera2FaceView) mRootView.findViewById(R.id.face_view);
- mFaceView.initMode();
- }
-
- private void initSettingsMenu() {
- mSettingsIcon = (ImageView) mRootView.findViewById(R.id.settings);
- mSettingsIcon.setImageResource(R.drawable.settings);
- mSettingsIcon.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- openSettingsMenu();
- }
- });
- }
-
- private void initPauseButton() {
- mRecordingTimeView = (TextView) mRootView.findViewById(R.id.recording_time);
- mRecordingTimeRect = (RotateLayout) mRootView.findViewById(R.id.multi_recording_time_rect);
- mTimeLapseLabel = mRootView.findViewById(R.id.time_lapse_label);
-
- mPauseButton = (PauseButton) mRootView.findViewById(R.id.video_pause);
- mPauseButton.setOnPauseButtonListener(this);
- }
-
- private void hideMenuButton() {
- if (mFlashButton == null) {
- mFlashButton = (FlashToggleButton) mRootView.findViewById(R.id.flash_button);
- }
- if (mFilterModeSwitcher == null) {
- mFilterModeSwitcher = mRootView.findViewById(R.id.filter_mode_switcher);
- }
- if (mSceneModeSwitcher == null) {
- mSceneModeSwitcher = mRootView.findViewById(R.id.scene_mode_switcher);
- }
- mFlashButton.setVisibility(View.INVISIBLE);
- mFilterModeSwitcher.setVisibility(View.INVISIBLE);
- mSceneModeSwitcher.setVisibility(View.INVISIBLE);
- }
-
- private void initVideoButton() {
- mVideoButton = (ImageView) mRootView.findViewById(R.id.video_button);
- mVideoButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- cancelCountDown();
- mModule.onVideoButtonClick();
- }
- });
- }
-
- private void initVideoMuteButton() {
- mMuteButton = (RotateImageView)mRootView.findViewById(R.id.mute_button);
- mMuteButton.setVisibility(View.VISIBLE);
- setMuteButtonResource(!mModule.isAudioMute());
- mMuteButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- boolean isEnabled = !mModule.isAudioMute();
- mModule.setMute(isEnabled, true);
- setMuteButtonResource(!isEnabled);
- }
- });
- }
-
- private void initializeCountDown() {
- mActivity.getLayoutInflater().inflate(R.layout.count_down_to_capture,
- (ViewGroup) mRootView, true);
- mCountDownView = (CountDownView) (mRootView.findViewById(R.id.count_down_to_capture));
- mCountDownView.setCountDownFinishedListener((CountDownView.OnCountDownFinishedListener) mModule);
- mCountDownView.bringToFront();
- mCountDownView.setOrientation(mOrientation);
- }
-
- private void setMuteButtonResource(boolean isUnMute) {
- if(isUnMute) {
- mMuteButton.setImageResource(R.drawable.ic_unmuted_button);
- } else {
- mMuteButton.setImageResource(R.drawable.ic_muted_button);
- }
- }
-
- public void setRecordingTime(String text) {
- mRecordingTimeView.setText(text);
- }
-
- public void setRecordingTimeTextColor(int color) {
- mRecordingTimeView.setTextColor(color);
- }
-
- public void resetPauseButton() {
- mRecordingTimeView.setCompoundDrawablesWithIntrinsicBounds(
- R.drawable.ic_recording_indicator, 0, 0, 0);
- mPauseButton.setPaused(false);
- }
-
- public void showRecordingUI(boolean recording) {
- if (recording) {
- mCameraControls.setVideoMode(true);
- mVideoButton.setImageResource(R.drawable.video_stop);
- mPauseButton.setVisibility(View.VISIBLE);
- mRecordingTimeView.setText("00:00");
- mRecordingTimeRect.setVisibility(View.VISIBLE);
- mMuteButton.setVisibility(View.INVISIBLE);
- setMuteButtonResource(!mModule.isAudioMute());
- showTimeLapseUI(false);
- mShutterButton.setVisibility(View.VISIBLE);
- mSettingsIcon.setVisibility(View.INVISIBLE);
- } else {
- //mFlashButton.setVisibility(View.VISIBLE);
- //mFlashButton.init(true);
- mCameraControls.setVideoMode(false);
- mPauseButton.setVisibility(View.INVISIBLE);
- mVideoButton.setImageResource(R.drawable.btn_new_shutter_video);
- mRecordingTimeRect.setVisibility(View.GONE);
- mMuteButton.setVisibility(View.INVISIBLE);
- mShutterButton.setVisibility(View.INVISIBLE);
- mSettingsIcon.setVisibility(View.VISIBLE);
- }
- }
-
- public void showTimeLapseUI(boolean enable) {
- if (mTimeLapseLabel != null) {
- mTimeLapseLabel.setVisibility(enable ? View.VISIBLE : View.GONE);
- }
- }
-
- /**
- * Enables or disables the shutter button.
- */
- public void enableShutter(boolean enabled) {
- if (mShutterButton != null) {
- mShutterButton.setEnabled(enabled);
- }
- }
-
- public void showRelatedIcons(MultiCameraModule.CameraMode mode) {
- //common settings
- mShutterButton.setVisibility(View.VISIBLE);
- //settings for each mode
- switch (mode) {
- case DEFAULT:
- mCameraControls.setVideoMode(false);
- mVideoButton.setVisibility(View.INVISIBLE);
- mMuteButton.setVisibility(View.INVISIBLE);
- mPauseButton.setVisibility(View.INVISIBLE);
- break;
- case VIDEO:
- mVideoButton.setVisibility(View.VISIBLE);
- mShutterButton.setVisibility(View.INVISIBLE);
- break;
- default:
- break;
- }
- }
-
- public void setOrientation(int orientation, boolean animation) {
- mOrientation = orientation;
- mCameraControls.setOrientation(orientation, animation);
- if (mRecordingTimeRect != null) {
- mRecordingTimeView.setRotation(-orientation);
- }
- if (mCountDownView != null)
- mCountDownView.setOrientation(orientation);
-
- }
-
- public int getOrientation() {
- return mOrientation;
- }
-
- public void cancelCountDown() {
- if (mCountDownView == null) return;
- mCountDownView.cancelCountDown();
- }
-
- public void initCountDownView() {
- if (mCountDownView == null) {
- initializeCountDown();
- } else {
- mCountDownView.initSoundPool();
- }
- }
-
- public void onCameraOpened(int cameraId) {
- mGestures.setMultiCameraUI(this);
- }
-
- public void swipeCameraMode(int move) {
- if (!mModule.getCameraModeSwitcherAllowed()) {
- return;
- }
- int index = mModule.getCurrentModeIndex() + move;
- int modeListSize = mModule.getCameraModeList().size();
- if (index >= modeListSize || index == -1) {
- return;
- }
- int mode = index % modeListSize;
- mModule.setCameraModeSwitcherAllowed(false);
- mCameraModeAdapter.setSelectedPosition(mode);
- mModeSelectLayout.smoothScrollToPosition(mode);
- mModule.selectCameraMode(mode);
- }
-
- public boolean isShutterEnabled() {
- return mShutterButton.isEnabled();
- }
-
- public ArrayList<AutoFitSurfaceView> getSurfaceViewList () {
- return mSurfaceViewList;
- }
-
-
- private void openSettingsMenu() {
- Intent intent = new Intent(mActivity, MultiSettingsActivity.class);
- intent.putExtra(MultiSettingsActivity.CAMERA_MODULE, mModule.getCurrenCameraMode());
- intent.putExtra(MultiSettingsActivity.CAMERA_ID_LISTS, mModule.getOpenCameraIdList());
- mActivity.startActivity(intent);
- }
-
- private void previewUIReady() {
- if((mMainSurfaceHolder != null && mMainSurfaceHolder.getSurface().isValid())) {
- mModule.onPreviewUIReady();
- if (mModule.isRecordingVideo() && mThumbnail != null){
- mThumbnail.setVisibility(View.INVISIBLE);
- mThumbnail = null;
- mActivity.updateThumbnail(mThumbnail);
- } else if (!mModule.isRecordingVideo()){
- if (mThumbnail == null)
- mThumbnail = (ImageView) mRootView.findViewById(R.id.preview_thumb);
- mActivity.updateThumbnail(mThumbnail);
- }
- }
- }
-
- private SurfaceHolder.Callback mMainSurfaceHolderCallback = new SurfaceHolder.Callback() {
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- Log.v(TAG, "surfaceChanged: width =" + width + ", height = " + height);
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- Log.v(TAG, "mMainSurfaceHolderCallback surfaceCreated");
- mMainSurfaceHolder = holder;
- previewUIReady();
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.v(TAG, "mMainSurfaceHolderCallback surfaceDestroyed");
- mMainSurfaceHolder = null;
- }
- };
-
- private SurfaceHolder.Callback mFirstHolderCallback = new SurfaceHolder.Callback() {
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- Log.v(TAG, "mFirstHolderCallback surfaceChanged: w : h =" + width + "x" + height);
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- Log.v(TAG, "mFirstHolderCallback surfaceCreated");
- mFirstSurfaceHolder = holder;
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.v(TAG, "mFirstHolderCallback surfaceDestroyed");
- mFirstSurfaceHolder = null;
- }
- };
-
- private SurfaceHolder.Callback mSecondHolderCallback = new SurfaceHolder.Callback() {
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- Log.v(TAG, "mSecondHolderCallback surfaceChanged: w : h =" + width + "x" + height);
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- Log.v(TAG, "mSecondHolderCallback surfaceCreated");
- mSecondSurfaceHolder = holder;
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.v(TAG, "mSecondHolderCallback surfaceDestroyed");
- mSecondSurfaceHolder = null;
- }
- };
-
- @Override
- public void onSingleTapUp(View view, int x, int y) {
- mModule.onSingleTapUp(view, x, y);
- }
-
- @Override
- public void onLongPress(View view, int x, int y) {
- mModule.onLongPress(view, x, y);
- }
+/* + * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.camera.multi; + +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.drawable.AnimationDrawable; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.view.SurfaceHolder; +import android.util.Log; + +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; + +import com.android.camera.CameraActivity; +import com.android.camera.PauseButton; +import com.android.camera.PreviewGestures; +import com.android.camera.data.Camera2ModeAdapter; +import com.android.camera.ShutterButton; +import com.android.camera.ui.AutoFitSurfaceView; +import com.android.camera.ui.CameraControls; +import com.android.camera.ui.Camera2FaceView; +import com.android.camera.ui.CountDownView; +import com.android.camera.ui.FlashToggleButton; +import com.android.camera.ui.OneUICameraControls; +import com.android.camera.ui.RenderOverlay; +import com.android.camera.ui.RotateImageView; +import com.android.camera.ui.RotateLayout; + +import org.codeaurora.snapcam.R; + +import java.util.ArrayList; + +public class MultiCameraUI implements PreviewGestures.SingleTapListener, + PauseButton.OnPauseButtonListener { + + private static final String TAG = "SnapCam_MultiCameraUI"; + + private static final int MAX_NUM_CAM = 16; + + private static final int PREVIEW_WIDTH = 1024; + private static final int PREVIEW_HIEGHT = 768; + + private CameraActivity mActivity; + private View mRootView; + private MultiCameraModule mModule; + private PreviewGestures mGestures; + + /** + * An {@link AutoFitTextureView} for camera preview. + */ + private AutoFitSurfaceView mMainPreviewSurface; + private AutoFitSurfaceView mFirstPreviewSurface; + private AutoFitSurfaceView mSecondPreviewSurface; + private AutoFitSurfaceView mPreviewSurface; + private ArrayList<AutoFitSurfaceView> mSurfaceViewList = new ArrayList(); + + private SurfaceHolder mMainSurfaceHolder; + private SurfaceHolder mFirstSurfaceHolder; + private SurfaceHolder mSecondSurfaceHolder; + + private RenderOverlay mRenderOverlay; + private ShutterButton mShutterButton; + private PauseButton mPauseButton; + private RotateImageView mMuteButton; + private ImageView mVideoButton; + private ImageView mThumbnail; + private ImageView mSettingsIcon; + + private CountDownView mCountDownView; + private OneUICameraControls mCameraControls; + private Camera2FaceView mFaceView; + + private TextView mRecordingTimeView; + private View mTimeLapseLabel; + private RotateLayout mRecordingTimeRect; + + private FlashToggleButton mFlashButton; + private View mFilterModeSwitcher; + private View mSceneModeSwitcher; + + private int mOrientation; + + private RecyclerView mModeSelectLayout; + private Camera2ModeAdapter mCameraModeAdapter; + + private int mPreviewWidths[] = new int[MAX_NUM_CAM]; + private int mPreviewHeights[] = new int[MAX_NUM_CAM]; + + public MultiCameraUI(CameraActivity activity, final MultiCameraModule module, View parent) { + mActivity = activity; + mModule = module; + mRootView = parent; + mActivity.getLayoutInflater().inflate(R.layout.multi_camera_module, + (ViewGroup) mRootView, true); + + initPreviewSurface(); + initCameraControls(); + initShutterButton(); + initVideoButton(); + initVideoMuteButton(); + initFlashButton(); + initializeThumbnail(); + hideMenuButton(); + initPauseButton(); + initSettingsMenu(); + initModeSelectLayout(); + } + + @Override + public void onButtonPause() { + mRecordingTimeView.setCompoundDrawablesWithIntrinsicBounds( + R.drawable.ic_pausing_indicator, 0, 0, 0); + mModule.onButtonPause(); + } + + @Override + public void onButtonContinue() { + mRecordingTimeView.setCompoundDrawablesWithIntrinsicBounds( + R.drawable.ic_recording_indicator, 0, 0, 0); + mModule.onButtonContinue(); + } + + public void showSurfaceView(int index) { + Log.d(TAG, "showSurfaceView" + mPreviewWidths[index] + " " + mPreviewHeights[index]); + mSurfaceViewList.get(index).getHolder().setFixedSize(mPreviewWidths[index], mPreviewHeights[index]); + mSurfaceViewList.get(index).setAspectRatio(mPreviewHeights[index], mPreviewWidths[index]); + mSurfaceViewList.get(index).setVisibility(View.VISIBLE); + } + + public boolean setPreviewSize(int index, int width, int height) { + Log.d(TAG, "setPreviewSize " + width + " " + height); + boolean changed = (width != mPreviewWidths[index]) || (height != mPreviewHeights[index]); + mPreviewWidths[index] = width; + mPreviewHeights[index] = height; + if (changed) { + showSurfaceView(index); + } + return changed; + } + + private void initPreviewSurface() { + // Multi camera preview + mMainPreviewSurface = (AutoFitSurfaceView) mRootView.findViewById(R.id.main_preview_content); + mFirstPreviewSurface = (AutoFitSurfaceView) mRootView.findViewById(R.id.first_preview_content); + mSecondPreviewSurface = (AutoFitSurfaceView) mRootView.findViewById(R.id.second_preview_content); + + mSurfaceViewList.add(mMainPreviewSurface); + mSurfaceViewList.add(mFirstPreviewSurface); + mSurfaceViewList.add(mSecondPreviewSurface); + + mMainSurfaceHolder = mMainPreviewSurface.getHolder(); + mMainSurfaceHolder.addCallback(mMainSurfaceHolderCallback); + mFirstSurfaceHolder = mFirstPreviewSurface.getHolder(); + mFirstSurfaceHolder.addCallback(mFirstHolderCallback); + mSecondSurfaceHolder = mSecondPreviewSurface.getHolder(); + mSecondSurfaceHolder.addCallback(mSecondHolderCallback); + + mMainPreviewSurface.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, + int bottom, int oldLeft, int oldTop, int oldRight, + int oldBottom) { + int width = right - left; + int height = bottom - top; + } + }); + } + + private void initShutterButton() { + mShutterButton = (ShutterButton) mRootView.findViewById(R.id.shutter_button); + mShutterButton.setOnShutterButtonListener(mModule); + mShutterButton.setImageResource(R.drawable.btn_new_shutter); + } + + private void initializeThumbnail() { + if (mThumbnail == null) { + mThumbnail = (ImageView) mRootView.findViewById(R.id.preview_thumb); + } + mActivity.updateThumbnail(mThumbnail); + mThumbnail.setVisibility(View.INVISIBLE); + mThumbnail.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (!CameraControls.isAnimating() && !mModule.isTakingPicture() && + !mModule.isRecordingVideo()) { + mActivity.gotoGallery(); + } + } + }); + } + + private void initModeSelectLayout() { + mRenderOverlay = (RenderOverlay) mRootView.findViewById(R.id.render_overlay); + mModeSelectLayout = (RecyclerView) mRootView.findViewById(R.id.mode_select_layout); + mModeSelectLayout.setLayoutManager(new LinearLayoutManager(mActivity, + LinearLayoutManager.HORIZONTAL, false)); + mCameraModeAdapter = new Camera2ModeAdapter(mModule.getCameraModeList(), + mModule.getCameraModeIconList()); + mCameraModeAdapter.setSelectedPosition(1); + mCameraModeAdapter.setOnItemClickListener(mModule.getModeItemClickListener()); + mModeSelectLayout.setAdapter(mCameraModeAdapter); + mModeSelectLayout.setVisibility(View.INVISIBLE); + + if (mGestures == null) { + // this will handle gesture disambiguation and dispatching + mGestures = new PreviewGestures(mActivity, this, null, null, null); + mRenderOverlay.setGestures(mGestures); + } + + mGestures.setRenderOverlay(mRenderOverlay); + mRenderOverlay.requestLayout(); + mActivity.setPreviewGestures(mGestures); + } + + private void initCameraControls() { + mCameraControls = (OneUICameraControls) mRootView.findViewById(R.id.camera_controls); + mFaceView = (Camera2FaceView) mRootView.findViewById(R.id.face_view); + mFaceView.initMode(); + } + + private void initSettingsMenu() { + mSettingsIcon = (ImageView) mRootView.findViewById(R.id.settings); + mSettingsIcon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + openSettingsMenu(); + } + }); + } + + private void initPauseButton() { + mRecordingTimeView = (TextView) mRootView.findViewById(R.id.recording_time); + mRecordingTimeRect = (RotateLayout) mRootView.findViewById(R.id.multi_recording_time_rect); + mTimeLapseLabel = mRootView.findViewById(R.id.time_lapse_label); + + mPauseButton = (PauseButton) mRootView.findViewById(R.id.video_pause); + mPauseButton.setOnPauseButtonListener(this); + } + + private void initFlashButton() { + if (mFlashButton == null) { + mFlashButton = (FlashToggleButton) mRootView.findViewById(R.id.flash_button); + } + mFlashButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mFlashButton.handleClick(); + } + }); + } + + private void hideMenuButton() { + if (mFilterModeSwitcher == null) { + mFilterModeSwitcher = mRootView.findViewById(R.id.filter_mode_switcher); + } + if (mSceneModeSwitcher == null) { + mSceneModeSwitcher = mRootView.findViewById(R.id.scene_mode_switcher); + } + mFlashButton.setVisibility(View.INVISIBLE); + mFilterModeSwitcher.setVisibility(View.INVISIBLE); + mSceneModeSwitcher.setVisibility(View.INVISIBLE); + } + + private void initVideoButton() { + mVideoButton = (ImageView) mRootView.findViewById(R.id.video_button); + mVideoButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + cancelCountDown(); + mModule.onVideoButtonClick(); + } + }); + } + + private void initVideoMuteButton() { + mMuteButton = (RotateImageView)mRootView.findViewById(R.id.mute_button); + mMuteButton.setVisibility(View.VISIBLE); + setMuteButtonResource(!mModule.isAudioMute()); + mMuteButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean isEnabled = !mModule.isAudioMute(); + mModule.setMute(isEnabled, true); + setMuteButtonResource(!isEnabled); + } + }); + } + + private void initializeCountDown() { + mActivity.getLayoutInflater().inflate(R.layout.count_down_to_capture, + (ViewGroup) mRootView, true); + mCountDownView = (CountDownView) (mRootView.findViewById(R.id.count_down_to_capture)); + mCountDownView.setCountDownFinishedListener((CountDownView.OnCountDownFinishedListener) mModule); + mCountDownView.bringToFront(); + mCountDownView.setOrientation(mOrientation); + } + + private void setMuteButtonResource(boolean isUnMute) { + if(isUnMute) { + mMuteButton.setImageResource(R.drawable.ic_unmuted_button); + } else { + mMuteButton.setImageResource(R.drawable.ic_muted_button); + } + } + + public void setRecordingTime(String text) { + mRecordingTimeView.setText(text); + } + + public void setRecordingTimeTextColor(int color) { + mRecordingTimeView.setTextColor(color); + } + + public void resetPauseButton() { + mRecordingTimeView.setCompoundDrawablesWithIntrinsicBounds( + R.drawable.ic_recording_indicator, 0, 0, 0); + mPauseButton.setPaused(false); + } + + public void showRecordingUI(boolean recording) { + if (recording) { + mCameraControls.setVideoMode(true); + mVideoButton.setImageResource(R.drawable.shutter_button_video_stop); + mPauseButton.setVisibility(View.VISIBLE); + mRecordingTimeView.setText("00:00"); + mRecordingTimeRect.setVisibility(View.VISIBLE); + mMuteButton.setVisibility(View.INVISIBLE); + setMuteButtonResource(!mModule.isAudioMute()); + showTimeLapseUI(false); + mShutterButton.setVisibility(View.VISIBLE); + mSettingsIcon.setVisibility(View.INVISIBLE); + } else { + //mFlashButton.setVisibility(View.VISIBLE); + //mFlashButton.init(true); + mCameraControls.setVideoMode(false); + mPauseButton.setVisibility(View.INVISIBLE); + mVideoButton.setImageResource(R.drawable.btn_new_shutter_video); + mRecordingTimeRect.setVisibility(View.GONE); + mMuteButton.setVisibility(View.INVISIBLE); + mShutterButton.setVisibility(View.INVISIBLE); + mSettingsIcon.setVisibility(View.VISIBLE); + } + } + + public void showTimeLapseUI(boolean enable) { + if (mTimeLapseLabel != null) { + mTimeLapseLabel.setVisibility(enable ? View.VISIBLE : View.GONE); + } + } + + /** + * Enables or disables the shutter button. + */ + public void enableShutter(boolean enabled) { + if (mShutterButton != null) { + mShutterButton.setEnabled(enabled); + } + } + + public void showRelatedIcons(MultiCameraModule.CameraMode mode) { + //common settings + mShutterButton.setVisibility(View.VISIBLE); + //settings for each mode + switch (mode) { + case DEFAULT: + mCameraControls.setVideoMode(false); + mVideoButton.setVisibility(View.INVISIBLE); + mMuteButton.setVisibility(View.INVISIBLE); + mPauseButton.setVisibility(View.INVISIBLE); + break; + case VIDEO: + mVideoButton.setVisibility(View.VISIBLE); + mShutterButton.setVisibility(View.INVISIBLE); + break; + default: + break; + } + } + + public void setOrientation(int orientation, boolean animation) { + mOrientation = orientation; + mCameraControls.setOrientation(orientation, animation); + if (mRecordingTimeRect != null) { + mRecordingTimeView.setRotation(-orientation); + } + if (mCountDownView != null) + mCountDownView.setOrientation(orientation); + + } + + public int getOrientation() { + return mOrientation; + } + + public void cancelCountDown() { + if (mCountDownView == null) return; + mCountDownView.cancelCountDown(); + } + + public void initCountDownView() { + if (mCountDownView == null) { + initializeCountDown(); + } else { + mCountDownView.initSoundPool(); + } + } + + public void onCameraOpened(int cameraId) { + mGestures.setMultiCameraUI(this); + } + + public void swipeCameraMode(int move) { + if (!mModule.getCameraModeSwitcherAllowed()) { + return; + } + int index = mModule.getCurrentModeIndex() + move; + int modeListSize = mModule.getCameraModeList().size(); + if (index >= modeListSize || index == -1) { + return; + } + int mode = index % modeListSize; + mModule.setCameraModeSwitcherAllowed(false); + mCameraModeAdapter.setSelectedPosition(mode); + mModeSelectLayout.smoothScrollToPosition(mode); + mModule.selectCameraMode(mode); + } + + public boolean isShutterEnabled() { + return mShutterButton.isEnabled(); + } + + public ArrayList<AutoFitSurfaceView> getSurfaceViewList () { + return mSurfaceViewList; + } + + + private void openSettingsMenu() { + Intent intent = new Intent(mActivity, MultiSettingsActivity.class); + intent.putExtra(MultiSettingsActivity.CAMERA_MODULE, mModule.getCurrenCameraMode()); + intent.putExtra(MultiSettingsActivity.CAMERA_ID_LISTS, mModule.getOpenCameraIdList()); + mActivity.startActivity(intent); + } + + private void previewUIReady() { + if((mMainSurfaceHolder != null && mMainSurfaceHolder.getSurface().isValid())) { + mModule.onPreviewUIReady(); + if (mModule.isRecordingVideo() && mThumbnail != null){ + mThumbnail.setVisibility(View.INVISIBLE); + mThumbnail = null; + mActivity.updateThumbnail(mThumbnail); + } else if (!mModule.isRecordingVideo()){ + if (mThumbnail == null) + mThumbnail = (ImageView) mRootView.findViewById(R.id.preview_thumb); + mActivity.updateThumbnail(mThumbnail); + } + } + } + + private SurfaceHolder.Callback mMainSurfaceHolderCallback = new SurfaceHolder.Callback() { + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + Log.v(TAG, "surfaceChanged: width =" + width + ", height = " + height); + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + Log.v(TAG, "mMainSurfaceHolderCallback surfaceCreated"); + mMainSurfaceHolder = holder; + previewUIReady(); + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + Log.v(TAG, "mMainSurfaceHolderCallback surfaceDestroyed"); + mMainSurfaceHolder = null; + } + }; + + private SurfaceHolder.Callback mFirstHolderCallback = new SurfaceHolder.Callback() { + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + Log.v(TAG, "mFirstHolderCallback surfaceChanged: w : h =" + width + "x" + height); + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + Log.v(TAG, "mFirstHolderCallback surfaceCreated"); + mFirstSurfaceHolder = holder; + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + Log.v(TAG, "mFirstHolderCallback surfaceDestroyed"); + mFirstSurfaceHolder = null; + } + }; + + private SurfaceHolder.Callback mSecondHolderCallback = new SurfaceHolder.Callback() { + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + Log.v(TAG, "mSecondHolderCallback surfaceChanged: w : h =" + width + "x" + height); + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + Log.v(TAG, "mSecondHolderCallback surfaceCreated"); + mSecondSurfaceHolder = holder; + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + Log.v(TAG, "mSecondHolderCallback surfaceDestroyed"); + mSecondSurfaceHolder = null; + } + }; + + @Override + public void onSingleTapUp(View view, int x, int y) { + mModule.onSingleTapUp(view, x, y); + } + + @Override + public void onLongPress(View view, int x, int y) { + mModule.onLongPress(view, x, y); + } } diff --git a/src/com/android/camera/multi/MultiCaptureModule.java b/src/com/android/camera/multi/MultiCaptureModule.java index cf629daa1..34065150f 100755..100644 --- a/src/com/android/camera/multi/MultiCaptureModule.java +++ b/src/com/android/camera/multi/MultiCaptureModule.java @@ -1,824 +1,824 @@ -/*
- * Copyright (c) 2019 The Linux Foundation. All rights reserved.
- * Not a Contribution.
- *
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.camera.multi;
-
-import android.content.Context;
-import android.content.ContentResolver;
-import android.content.SharedPreferences;
-import android.graphics.ImageFormat;
-import android.graphics.Point;
-import android.hardware.camera2.CameraDevice;
-import android.hardware.camera2.CameraManager;
-import android.hardware.camera2.CameraAccessException;
-import android.hardware.camera2.CameraCaptureSession;
-import android.hardware.camera2.CameraCharacteristics;
-import android.hardware.camera2.CaptureFailure;
-import android.hardware.camera2.CaptureRequest;
-import android.hardware.camera2.CaptureResult;
-import android.hardware.camera2.TotalCaptureResult;
-import android.hardware.camera2.params.OutputConfiguration;
-import android.hardware.camera2.params.SessionConfiguration;
-import android.hardware.camera2.params.StreamConfigurationMap;
-import android.media.CameraProfile;
-import android.media.Image;
-import android.media.ImageReader;
-import android.net.Uri;
-import android.os.Handler;
-import android.os.HandlerThread;
-import android.os.Looper;
-import android.os.Message;
-import android.util.Log;
-import android.util.Size;
-import android.view.Surface;
-import android.view.SurfaceHolder;
-import android.widget.Toast;
-
-import java.nio.ByteBuffer;
-import java.util.concurrent.Executor;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.TimeUnit;
-import java.util.Arrays;
-import java.util.ArrayList;
-import java.util.List;
-
-import com.android.camera.CameraActivity;
-import com.android.camera.CaptureModule;
-import com.android.camera.ComboPreferences;
-import com.android.camera.MediaSaveService;
-import com.android.camera.Exif;
-import com.android.camera.SoundClips;
-import com.android.camera.exif.ExifInterface;
-import com.android.camera.PhotoModule.NamedImages;
-import com.android.camera.PhotoModule.NamedImages.NamedEntity;
-import com.android.camera.util.CameraUtil;
-import com.android.camera.util.PersistUtil;
-import org.codeaurora.snapcam.R;
-
-public class MultiCaptureModule implements MultiCamera {
-
- private static final String TAG = "SnapCam_MultiCaptureModule";
-
- private static final boolean DEBUG =
- (PersistUtil.getCamera2Debug() == PersistUtil.CAMERA2_DEBUG_DUMP_LOG) ||
- (PersistUtil.getCamera2Debug() == PersistUtil.CAMERA2_DEBUG_DUMP_ALL);
-
- private static final int WAIT_SURFACE = 0;
- private static final int OPEN_CAMERA = 1;
-
- private int mCameraListIndex = 0;
-
- private static final int MAX_NUM_CAM = 16;
-
- private CameraActivity mActivity;
- private MultiCameraUI mMultiCameraUI;
- private MultiCameraModule mMultiCameraModule;
- private CameraDevice[] mCameraDevices = new CameraDevice[MAX_NUM_CAM];
- private SharedPreferences mLocalSharedPref;
- private ArrayList<CameraCharacteristics> mCharacteristics;
-
- private ArrayList<String> mCameraIDList = new ArrayList<>();
- private CameraCaptureSession[] mCameraCaptureSessions = new CameraCaptureSession[MAX_NUM_CAM];
- private ImageReader[] mImageReaders = new ImageReader[MAX_NUM_CAM];
-
- private Size mPreviewSizes[] = new Size[MAX_NUM_CAM];
- private Size mPictureSizes[] = new Size[MAX_NUM_CAM];
- private int[][] mMaxPreviewSize = new int[MAX_NUM_CAM][];
-
- private Handler mCameraHandler;
- private HandlerThread mCameraThread;
-
- private static final CaptureRequest.Key<Byte> override_resource_cost_validation =
- new CaptureRequest.Key<>(
- "org.codeaurora.qcamera3.sessionParameters.overrideResourceCostValidation",
- byte.class);
-
- /**
- * {@link CaptureRequest.Builder} for the camera preview
- */
- private CaptureRequest.Builder[] mPreviewRequestBuilders = new CaptureRequest.Builder[MAX_NUM_CAM];
- /**
- * {@link CaptureRequest} generated by {@link #mPreviewRequestBuilder}
- */
- private CaptureRequest mPreviewRequest;
- /**
- * A {@link Semaphore} make sure the camera open callback happens first before closing the
- * camera.
- */
- private Semaphore mCameraOpenCloseLock = new Semaphore(3);
-
- /**
- * Orientation of the camera sensor
- */
- private int mSensorOrientation;
-
- private long mCaptureStartTime;
- private NamedImages mNamedImages;
- private ContentResolver mContentResolver;
- private SoundClips.Player mSoundPlayer;
-
- public MultiCaptureModule(CameraActivity activity, MultiCameraUI ui, MultiCameraModule module) {
- mActivity = activity;
- mMultiCameraUI = ui;
- mMultiCameraModule = module;
- mContentResolver = mActivity.getContentResolver();
- mNamedImages = new NamedImages();
- mLocalSharedPref = mActivity.getSharedPreferences(
- ComboPreferences.getLocalSharedPreferencesName(mActivity,
- "multi" + mMultiCameraModule.getCurrenCameraMode()), Context.MODE_PRIVATE);
- initCameraCharacteristics();
- startBackgroundThread();
- }
-
- @Override
- public void onResume(String[] ids) {
- for (String id : ids) {
- mCameraIDList.add(id);
- }
- // Set up sound playback for shutter button, video record and video stop
- if (mSoundPlayer == null) {
- mSoundPlayer = SoundClips.getPlayer(mActivity);
- }
- startBackgroundThread();
- initCameraCharacteristics();
- for (String id : ids) {
- int cameraId = Integer.parseInt(id);
- updatePictureSize(cameraId);
- int index = mCameraIDList.indexOf(id);
- mMultiCameraUI.setPreviewSize(index, mPreviewSizes[cameraId].getWidth(),
- mPreviewSizes[cameraId].getHeight());
- }
- }
-
- @Override
- public void onPause() {
- if (mSoundPlayer != null) {
- mSoundPlayer.release();
- mSoundPlayer = null;
- }
- closeCamera();
- if (mCameraIDList != null) {
- mCameraIDList.clear();
- }
- mCameraListIndex = 0;
- stopBackgroundThread();
- }
-
- @Override
- public boolean openCamera(String[] ids) {
- Message msg = Message.obtain();
- msg.what = OPEN_CAMERA;
- if (mCameraHandler != null) {
- mCameraHandler.sendMessage(msg);
- }
- return true;
- }
-
- @Override
- public void startPreview() {
-
- }
-
- @Override
- public void closeSession() {
-
- }
-
- @Override
- public void closeCamera() {
- Log.d(TAG, "closeCamera");
- /* no need to set this in the callback and handle asynchronously. This is the same
- reason as why we release the semaphore here, not in camera close callback function
- as we don't have to protect the case where camera open() gets called during camera
- close(). The low level framework/HAL handles the synchronization for open()
- happens after close() */
- try {
- // Close camera starting with AUX first
- for (int i = MAX_NUM_CAM - 1; i >= 0; i--) {
- if (null != mCameraDevices[i]) {
- if (!mCameraOpenCloseLock.tryAcquire(2000, TimeUnit.MILLISECONDS)) {
- Log.d(TAG, "Time out waiting to lock camera closing.");
- throw new RuntimeException("Time out waiting to lock camera closing");
- }
- Log.d(TAG, "Closing camera: " + mCameraDevices[i].getId());
- mCameraDevices[i].close();
- mCameraDevices[i] = null;
- mCameraCaptureSessions[i] = null;
- }
- if (null != mImageReaders[i]) {
- mImageReaders[i].close();
- mImageReaders[i] = null;
- }
- }
- } catch (InterruptedException e) {
- mCameraOpenCloseLock.release();
- throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } finally {
- mCameraOpenCloseLock.release();
- }
- }
-
- private void openCameraInSequence(String id) {
- CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
- CameraCharacteristics characteristics = null;
- try {
- characteristics = manager.getCameraCharacteristics(id);
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
- Log.d(TAG, "openCameraInSequence " + id + ", mSensorOrientation :" + mSensorOrientation);
- try {
- if (!mCameraOpenCloseLock.tryAcquire(5000, TimeUnit.MILLISECONDS)) {
- Log.d(TAG, "Time out waiting to lock camera opening.");
- throw new RuntimeException("Time out waiting to lock camera opening");
- }
- manager.openCamera(id, mStateCallback, mMultiCameraModule.getMyCameraHandler());
- } catch (CameraAccessException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- manager = null;
- characteristics = null;
- }
-
- private void startBackgroundThread() {
- if (mCameraThread == null) {
- mCameraThread = new HandlerThread("CameraBackground");
- mCameraThread.start();
- }
- if (mCameraHandler == null) {
- mCameraHandler = new MyCameraHandler(mCameraThread.getLooper());
- }
- }
-
- private void stopBackgroundThread() {
- mCameraThread.quitSafely();
- try {
- mCameraThread.join();
- mCameraThread = null;
- mCameraHandler = null;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- private Size[] getSupportedOutputSize(int cameraId, Class cl) {
- StreamConfigurationMap map = mCharacteristics.get(cameraId).get(
- CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
- Size[] normal = map.getOutputSizes(cl);
- Size[] high = map.getHighResolutionOutputSizes(ImageFormat.PRIVATE);
- Size[] ret = new Size[normal.length + high.length];
- System.arraycopy(normal, 0, ret, 0, normal.length);
- System.arraycopy(high, 0, ret, normal.length, high.length);
- return ret;
- }
-
- private Size getOptimalPreviewSize(int id, Size pictureSize, Size[] prevSizes) {
- Point[] points = new Point[prevSizes.length];
-
- double targetRatio = (double) pictureSize.getWidth() / pictureSize.getHeight();
- int index = 0;
- int point_max[] = mMaxPreviewSize[id];
- int max_size = -1;
- if (point_max != null){
- max_size = point_max[0] * point_max[1];
- }
- for (Size s : prevSizes) {
- if (max_size != -1){
- int size = s.getWidth() * s.getHeight();
- if (s.getWidth() == s.getHeight()){
- if (s.getWidth() > Math.max(point_max[0],point_max[1]))
- continue;
- } else if (size > max_size || size == 0) {
- continue;
- }
- }
- points[index++] = new Point(s.getWidth(), s.getHeight());
- }
-
- int optimalPickIndex = CameraUtil.getOptimalPreviewSize(mActivity, points, targetRatio);
- return (optimalPickIndex == -1) ? null :
- new Size(points[optimalPickIndex].x,points[optimalPickIndex].y);
- }
-
- private class MyCameraHandler extends Handler {
-
- public MyCameraHandler(Looper looper) {
- super(looper);
- }
-
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case WAIT_SURFACE:
- int id = msg.arg1;
- int index = mCameraIDList.indexOf(String.valueOf(id));
- Log.v(TAG, "WAIT_SURFACE id :" + id + ", index :" + index);
- if (index == -1) {
- break;
- }
- Surface surface = mMultiCameraUI.getSurfaceViewList().get(index)
- .getHolder().getSurface();
- if (surface.isValid()) {
- createCaptureSessions(id, surface);
- } else {
- mCameraHandler.sendMessageDelayed(msg, 200);
- Log.v(TAG, "Surface is invalid, wait more 200ms surfaceCreated");
- }
- break;
- case OPEN_CAMERA:
- if (mCameraListIndex == mCameraIDList.size()) {
- mCameraListIndex = 0;
- } else {
- String cameraId = mCameraIDList.get(mCameraListIndex);
- openCameraInSequence(cameraId);
- mCameraListIndex ++;
- Log.v(TAG, " OPEN_CAMERA cameraId :" + cameraId + ", mCameraListIndex :"
- + mCameraListIndex);
- }
- break;
- }
- }
- }
-
- private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
-
- @Override
- public void onOpened(CameraDevice cameraDevice) {
- final int id = Integer.parseInt(cameraDevice.getId());
- mCameraDevices[id] = cameraDevice;
- Log.d(TAG, "onOpened " + id);
- mCameraOpenCloseLock.release();
- //updatePictureSize(id);
- createCameraPreviewSession(id);
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mMultiCameraUI.onCameraOpened(id);
- }
- });
- }
-
- @Override
- public void onDisconnected(CameraDevice cameraDevice) {
- int id = Integer.parseInt(cameraDevice.getId());
- Log.d(TAG, "onDisconnected " + id);
- mCameraOpenCloseLock.release();
- mCameraDevices[id] = null;
- }
-
- @Override
- public void onError(CameraDevice cameraDevice, int error) {
- int id = Integer.parseInt(cameraDevice.getId());
- Log.e(TAG, "onError " + id + " " + error);
- mCameraOpenCloseLock.release();
-
- if (null != mActivity) {
- Toast.makeText(mActivity,"open camera error id =" + id,
- Toast.LENGTH_LONG).show();
- mActivity.finish();
- }
- }
-
- @Override
- public void onClosed(CameraDevice cameraDevice) {
- int id = Integer.parseInt(cameraDevice.getId());
- Log.d(TAG, "onClosed " + id);
- mCameraOpenCloseLock.release();
- mCameraDevices[id] = null;
- }
- };
-
- /**
- * Creates a new {@link CameraCaptureSession} for camera preview.
- */
- private void createCameraPreviewSession(int id) {
- // This is the output Surface we need to start preview.
- int index = mCameraIDList.indexOf(String.valueOf(id));
- Log.v(TAG, "createCameraPreviewSession id :" + id + ", index :" + index);
- Surface surface = mMultiCameraUI.getSurfaceViewList().get(index).getHolder().getSurface();
- if (surface.isValid()) {
- createCaptureSessions(id, surface);
- } else {
- Message msg = Message.obtain();
- msg.what = WAIT_SURFACE;
- msg.arg1 = id;
- mCameraHandler.sendMessageDelayed(msg, 200);
- Log.v(TAG, "Surface is invalid, wait 200ms surfaceCreated");
- }
- }
-
- private void createCaptureSessions(final int id, Surface surface) {
- try {
- // We set up a CaptureRequest.Builder with the output Surface.
- mPreviewRequestBuilders[id]
- = mCameraDevices[id].createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
- mPreviewRequestBuilders[id].addTarget(surface);
- mPreviewRequestBuilders[id].setTag(id);
- CameraCaptureSession.StateCallback stateCallback =
- new CameraCaptureSession.StateCallback() {
- @Override
- public void onConfigured(CameraCaptureSession cameraCaptureSession) {
- // The camera is already closed
- if (null == mCameraDevices[id]) {
- return;
- }
- Log.v(TAG, " CameraCaptureSession onConfigured id :" + id);
- // When the session is ready, we start displaying the preview.
- mCameraCaptureSessions[id] = cameraCaptureSession;
- try {
- // Auto focus should be continuous for camera preview.
- mPreviewRequestBuilders[id].set(CaptureRequest.CONTROL_AF_MODE,
- CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
- // Finally, we start displaying the camera preview.
- mCameraCaptureSessions[id].setRepeatingRequest(
- mPreviewRequestBuilders[id].build(),
- mCaptureCallback, mMultiCameraModule.getMyCameraHandler());
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
-
- Message msg = Message.obtain();
- msg.what = OPEN_CAMERA;
- if (mCameraHandler != null) {
- mCameraHandler.sendMessage(msg);
- }
- }
-
- @Override
- public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
- showToast("onConfigureFailed");
- }
- };
-
- try {
- final byte enable = 1;
- mPreviewRequestBuilders[id].set(override_resource_cost_validation, enable);
- Log.v(TAG, " set" + override_resource_cost_validation + " is 1");
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
-
- List<OutputConfiguration> outConfigurations = new ArrayList<>(2);
- outConfigurations.add(new OutputConfiguration(surface));
- outConfigurations.add(new OutputConfiguration(mImageReaders[id].getSurface()));
-
- SessionConfiguration sessionConfiguration = new SessionConfiguration(
- SessionConfiguration.SESSION_REGULAR, outConfigurations,
- new HandlerExecutor(mCameraHandler), stateCallback);
- sessionConfiguration.setSessionParameters(mPreviewRequestBuilders[id].build());
- mCameraDevices[id].createCaptureSession(sessionConfiguration);
- try {
- CaptureRequest captureRequest = sessionConfiguration.getSessionParameters();
- Log.v(TAG, " override_resource_cost_validation result: " +
- captureRequest.get(override_resource_cost_validation));
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
-
- private class HandlerExecutor implements Executor {
- private final Handler ihandler;
-
- public HandlerExecutor(Handler handler) {
- ihandler = handler;
- }
-
- @Override
- public void execute(Runnable runCmd) {
- ihandler.post(runCmd);
- }
- }
-
- /**
- * A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture.
- */
- private CameraCaptureSession.CaptureCallback mCaptureCallback
- = new CameraCaptureSession.CaptureCallback() {
-
- private void process(CaptureResult result) {
- Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
- Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
- if (DEBUG) {
- Log.v(TAG, "process afState :" + afState + ", aeState :" + aeState);
- }
- }
-
- @Override
- public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
- CaptureResult partialResult) {
- process(partialResult);
- }
-
- @Override
- public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
- TotalCaptureResult result) {
- process(result);
- }
-
- };
-
- private void updatePictureSize(int id) {
- String defaultSize = mActivity.getString(R.string.pref_multi_camera_picturesize_default);
- int index = mCameraIDList.indexOf(String.valueOf(id));
- String pictureSize = mLocalSharedPref.getString(
- MultiSettingsActivity.KEY_PICTURE_SIZES.get(index), defaultSize);
- mPictureSizes[id] = parsePictureSize(pictureSize);
- Log.v(TAG, " updatePictureSize size :" + mPictureSizes[id].getWidth() + "x"
- + mPictureSizes[id].getHeight());
- mImageReaders[id] = ImageReader.newInstance(mPictureSizes[id].getWidth(),
- mPictureSizes[id].getHeight(), ImageFormat.JPEG, /*maxImages*/2);
- mImageReaders[id].setOnImageAvailableListener(
- mOnImageAvailableListener, mMultiCameraModule.getMyCameraHandler());
-
- Size[] prevSizes = getSupportedOutputSize(id, SurfaceHolder.class);
- mPreviewSizes[id] = getOptimalPreviewSize(id, mPictureSizes[id], prevSizes);
- }
-
- private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
- = new ImageReader.OnImageAvailableListener() {
-
- @Override
- public void onImageAvailable(ImageReader reader) {
- Log.v(TAG, "onImageAvailable ...");
- Image image = reader.acquireNextImage();
- mCaptureStartTime = System.currentTimeMillis();
- mNamedImages.nameNewImage(mCaptureStartTime);
- NamedEntity name = mNamedImages.getNextNameEntity();
- String title = (name == null) ? null : name.title;
- long date = (name == null) ? -1 : name.date;
-
- byte[] bytes = getJpegData(image);
- Log.i(TAG, "image format:" + image.getFormat());
- if (image.getFormat() == ImageFormat.RAW10) {
- mActivity.getMediaSaveService().addRawImage(bytes, title,
- "raw");
- image.close();
- } else if (image.getFormat() == ImageFormat.YUV_420_888) {
- Log.d(TAG, "YUV buffer received" );
- image.close();
- } else {
- int orientation = 0;
- ExifInterface exif = null;
- if (image.getFormat() != ImageFormat.HEIC) {
- exif = Exif.getExif(bytes);
- orientation = Exif.getOrientation(exif);
- }
-
- String pictureFormat = "jpeg";
- if (image.getFormat() == ImageFormat.HEIC) {
- pictureFormat = "heic";
- }
- mActivity.getMediaSaveService().addImage(bytes, title, date,
- null, image.getWidth(), image.getHeight(), orientation, exif,
- mOnMediaSavedListener, mContentResolver, pictureFormat);
-
- if (image.getFormat() != ImageFormat.HEIC) {
- mActivity.updateThumbnail(bytes);
- }
- image.close();
- mMultiCameraModule.updateTakingPicture();
- }
- }
-
- };
-
- /**
- * Shows a {@link Toast} on the UI thread.
- * @param text The message to show
- */
- private void showToast(final String text) {
- if (mActivity != null) {
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
-
- private byte[] getJpegData(Image image) {
- ByteBuffer buffer = image.getPlanes()[0].getBuffer();
- byte[] bytes = new byte[buffer.remaining()];
- buffer.get(bytes);
- return bytes;
- }
-
- @Override
- public void onShutterButtonClick(String[] ids) {
- checkAndPlayShutterSound();
- mMultiCameraUI.enableShutter(false);
- boolean halZSLCheck = mLocalSharedPref.getBoolean(MultiSettingsActivity.KEY_HAL_ZAL, false);
- for (String id : ids) {
- try {
- int cameraId = Integer.parseInt(id);
- final CaptureRequest.Builder captureBuilder =
- mCameraDevices[cameraId].createCaptureRequest(
- CameraDevice.TEMPLATE_STILL_CAPTURE);
- captureBuilder.addTarget(mImageReaders[cameraId].getSurface());
- int index = mCameraIDList.indexOf(String.valueOf(cameraId));
- captureBuilder.addTarget(mMultiCameraUI.getSurfaceViewList().get(
- index).getHolder().getSurface());
- applySettingsForCapture(captureBuilder, cameraId);
- // Use the same AE and AF modes as the preview.
- captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
- CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
- if (halZSLCheck) {
- captureBuilder.set(CaptureRequest.CONTROL_ENABLE_ZSL, true);
- } else {
- captureBuilder.set(CaptureRequest.CONTROL_ENABLE_ZSL, false);
- }
- setAutoFlash(captureBuilder);
- // Orientation
- int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
- captureBuilder.set(CaptureRequest.JPEG_ORIENTATION,
- CameraUtil.getJpegRotation(cameraId, rotation));
- mCameraCaptureSessions[cameraId].capture(captureBuilder.build(),
- mCaptureStillCallback, mMultiCameraModule.getMyCameraHandler());
- Log.d(TAG, " cameraCaptureSession" + id + " captured ");
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
- }
-
- @Override
- public void onVideoButtonClick(String[] ids) {
-
- }
-
- @Override
- public void onButtonPause(String[] ids) {
-
- }
-
- @Override
- public void onButtonContinue(String[] ids) {
-
- }
-
- @Override
- public void onOrientationChanged(int orientation) {
-
- }
-
- @Override
- public boolean isRecordingVideo() {
- return false;
- }
-
- private void checkAndPlayShutterSound() {
- boolean isPlay = mLocalSharedPref.getBoolean(MultiSettingsActivity.KEY_SHUTTER_SOUND, true);
- if (mSoundPlayer != null && isPlay) {
- mSoundPlayer.play(SoundClips.SHUTTER_CLICK);
- }
- }
-
- private MediaSaveService.OnMediaSavedListener mOnMediaSavedListener =
- new MediaSaveService.OnMediaSavedListener() {
- @Override
- public void onMediaSaved(Uri uri) {
- if (uri != null) {
- mActivity.notifyNewMedia(uri);
- }
- }
- };
-
- private CameraCaptureSession.CaptureCallback mCaptureStillCallback
- = new CameraCaptureSession.CaptureCallback() {
-
- @Override
- public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
- TotalCaptureResult result) {
- Log.d(TAG, " mCaptureCallback onCaptureCompleted ");
- mMultiCameraModule.getMainHandler().post(new Runnable() {
- @Override
- public void run() {
- Log.d(TAG, " enable Shutter " );
- mMultiCameraUI.enableShutter(true);
- }
- });
- }
-
- @Override
- public void onCaptureFailed(CameraCaptureSession session,
- CaptureRequest request,
- CaptureFailure result) {
- Log.d(TAG, " mCaptureCallback onCaptureFailed " );
- }
-
-
- @Override
- public void onCaptureSequenceCompleted(CameraCaptureSession session, int
- sequenceId, long frameNumber) {
- Log.d(TAG, " mCaptureCallback onCaptureSequenceCompleted ");
- }
- };
-
- private void applySettingsForCapture(CaptureRequest.Builder builder, int id) {
- builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE);
- applyJpegQuality(builder);
- }
-
- private void applyJpegQuality(CaptureRequest.Builder request) {
- String defaultValue = mActivity.getString(R.string.pref_camera_jpegquality_default);
- String value = mLocalSharedPref.getString(MultiSettingsActivity.KEY_PICTURE_QUALITY,
- defaultValue);
- int jpegQuality = getQualityNumber(value);
- request.set(CaptureRequest.JPEG_QUALITY, (byte) jpegQuality);
- }
-
- private int getQualityNumber(String jpegQuality) {
- if (jpegQuality == null) {
- return 85;
- }
- try {
- int qualityPercentile = Integer.parseInt(jpegQuality);
- if (qualityPercentile >= 0 && qualityPercentile <= 100)
- return qualityPercentile;
- else
- return 85;
- } catch (NumberFormatException nfe) {
- //chosen quality is not a number, continue
- }
- int value = 0;
- switch (jpegQuality) {
- case "superfine":
- value = CameraProfile.QUALITY_HIGH;
- break;
- case "fine":
- value = CameraProfile.QUALITY_MEDIUM;
- break;
- case "normal":
- value = CameraProfile.QUALITY_LOW;
- break;
- default:
- return 85;
- }
- return CameraProfile.getJpegEncodingQualityParameter(value);
- }
-
- private Size parsePictureSize(String value) {
- int indexX = value.indexOf('x');
- int width = Integer.parseInt(value.substring(0, indexX));
- int height = Integer.parseInt(value.substring(indexX + 1));
- return new Size(width, height);
- }
-
- private void initCameraCharacteristics() {
- mCharacteristics = new ArrayList<>();
- CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
- try {
- String[] cameraIdList = manager.getCameraIdList();
- Log.d(TAG, "cameraIdList size =" + cameraIdList.length);
- for (int i = 0; i < cameraIdList.length; i++) {
- String cameraId = cameraIdList[i];
- CameraCharacteristics characteristics
- = manager.getCameraCharacteristics(cameraId);
- mCharacteristics.add(i, characteristics);
- try {
- mMaxPreviewSize[i] = characteristics.get(CaptureModule.max_preview_size);
- } catch (IllegalArgumentException e) {
- Log.e(TAG, "getMaxPreviewSize no vendorTag max_preview_size:");
- }
- if (mMaxPreviewSize[i] != null) {
- Log.d(TAG, " init cameraId :" + cameraId + ", i :" + i +
- ", maxPreviewSize :" + mMaxPreviewSize[i][0]+ "x" +
- mMaxPreviewSize[i][1]);
- }
- }
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
-
- private void setAutoFlash(CaptureRequest.Builder requestBuilder) {
- if (true) {
- requestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
- CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
- }
- }
+/* + * Copyright (c) 2019 The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.camera.multi; + +import android.content.Context; +import android.content.ContentResolver; +import android.content.SharedPreferences; +import android.graphics.ImageFormat; +import android.graphics.Point; +import android.hardware.camera2.CameraDevice; +import android.hardware.camera2.CameraManager; +import android.hardware.camera2.CameraAccessException; +import android.hardware.camera2.CameraCaptureSession; +import android.hardware.camera2.CameraCharacteristics; +import android.hardware.camera2.CaptureFailure; +import android.hardware.camera2.CaptureRequest; +import android.hardware.camera2.CaptureResult; +import android.hardware.camera2.TotalCaptureResult; +import android.hardware.camera2.params.OutputConfiguration; +import android.hardware.camera2.params.SessionConfiguration; +import android.hardware.camera2.params.StreamConfigurationMap; +import android.media.CameraProfile; +import android.media.Image; +import android.media.ImageReader; +import android.net.Uri; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Looper; +import android.os.Message; +import android.util.Log; +import android.util.Size; +import android.view.Surface; +import android.view.SurfaceHolder; +import android.widget.Toast; + +import java.nio.ByteBuffer; +import java.util.concurrent.Executor; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.List; + +import com.android.camera.CameraActivity; +import com.android.camera.CaptureModule; +import com.android.camera.ComboPreferences; +import com.android.camera.MediaSaveService; +import com.android.camera.Exif; +import com.android.camera.SoundClips; +import com.android.camera.exif.ExifInterface; +import com.android.camera.PhotoModule.NamedImages; +import com.android.camera.PhotoModule.NamedImages.NamedEntity; +import com.android.camera.util.CameraUtil; +import com.android.camera.util.PersistUtil; +import org.codeaurora.snapcam.R; + +public class MultiCaptureModule implements MultiCamera { + + private static final String TAG = "SnapCam_MultiCaptureModule"; + + private static final boolean DEBUG = + (PersistUtil.getCamera2Debug() == PersistUtil.CAMERA2_DEBUG_DUMP_LOG) || + (PersistUtil.getCamera2Debug() == PersistUtil.CAMERA2_DEBUG_DUMP_ALL); + + private static final int WAIT_SURFACE = 0; + private static final int OPEN_CAMERA = 1; + + private int mCameraListIndex = 0; + + private static final int MAX_NUM_CAM = 16; + + private CameraActivity mActivity; + private MultiCameraUI mMultiCameraUI; + private MultiCameraModule mMultiCameraModule; + private CameraDevice[] mCameraDevices = new CameraDevice[MAX_NUM_CAM]; + private SharedPreferences mLocalSharedPref; + private ArrayList<CameraCharacteristics> mCharacteristics; + + private ArrayList<String> mCameraIDList = new ArrayList<>(); + private CameraCaptureSession[] mCameraCaptureSessions = new CameraCaptureSession[MAX_NUM_CAM]; + private ImageReader[] mImageReaders = new ImageReader[MAX_NUM_CAM]; + + private Size mPreviewSizes[] = new Size[MAX_NUM_CAM]; + private Size mPictureSizes[] = new Size[MAX_NUM_CAM]; + private int[][] mMaxPreviewSize = new int[MAX_NUM_CAM][]; + + private Handler mCameraHandler; + private HandlerThread mCameraThread; + + private static final CaptureRequest.Key<Byte> override_resource_cost_validation = + new CaptureRequest.Key<>( + "org.codeaurora.qcamera3.sessionParameters.overrideResourceCostValidation", + byte.class); + + /** + * {@link CaptureRequest.Builder} for the camera preview + */ + private CaptureRequest.Builder[] mPreviewRequestBuilders = new CaptureRequest.Builder[MAX_NUM_CAM]; + /** + * {@link CaptureRequest} generated by {@link #mPreviewRequestBuilder} + */ + private CaptureRequest mPreviewRequest; + /** + * A {@link Semaphore} make sure the camera open callback happens first before closing the + * camera. + */ + private Semaphore mCameraOpenCloseLock = new Semaphore(3); + + /** + * Orientation of the camera sensor + */ + private int mSensorOrientation; + + private long mCaptureStartTime; + private NamedImages mNamedImages; + private ContentResolver mContentResolver; + private SoundClips.Player mSoundPlayer; + + public MultiCaptureModule(CameraActivity activity, MultiCameraUI ui, MultiCameraModule module) { + mActivity = activity; + mMultiCameraUI = ui; + mMultiCameraModule = module; + mContentResolver = mActivity.getContentResolver(); + mNamedImages = new NamedImages(); + mLocalSharedPref = mActivity.getSharedPreferences( + ComboPreferences.getLocalSharedPreferencesName(mActivity, + "multi" + mMultiCameraModule.getCurrenCameraMode()), Context.MODE_PRIVATE); + initCameraCharacteristics(); + startBackgroundThread(); + } + + @Override + public void onResume(String[] ids) { + for (String id : ids) { + mCameraIDList.add(id); + } + // Set up sound playback for shutter button, video record and video stop + if (mSoundPlayer == null) { + mSoundPlayer = SoundClips.getPlayer(mActivity); + } + startBackgroundThread(); + initCameraCharacteristics(); + for (String id : ids) { + int cameraId = Integer.parseInt(id); + updatePictureSize(cameraId); + int index = mCameraIDList.indexOf(id); + mMultiCameraUI.setPreviewSize(index, mPreviewSizes[cameraId].getWidth(), + mPreviewSizes[cameraId].getHeight()); + } + } + + @Override + public void onPause() { + if (mSoundPlayer != null) { + mSoundPlayer.release(); + mSoundPlayer = null; + } + closeCamera(); + if (mCameraIDList != null) { + mCameraIDList.clear(); + } + mCameraListIndex = 0; + stopBackgroundThread(); + } + + @Override + public boolean openCamera(String[] ids) { + Message msg = Message.obtain(); + msg.what = OPEN_CAMERA; + if (mCameraHandler != null) { + mCameraHandler.sendMessage(msg); + } + return true; + } + + @Override + public void startPreview() { + + } + + @Override + public void closeSession() { + + } + + @Override + public void closeCamera() { + Log.d(TAG, "closeCamera"); + /* no need to set this in the callback and handle asynchronously. This is the same + reason as why we release the semaphore here, not in camera close callback function + as we don't have to protect the case where camera open() gets called during camera + close(). The low level framework/HAL handles the synchronization for open() + happens after close() */ + try { + // Close camera starting with AUX first + for (int i = MAX_NUM_CAM - 1; i >= 0; i--) { + if (null != mCameraDevices[i]) { + if (!mCameraOpenCloseLock.tryAcquire(2000, TimeUnit.MILLISECONDS)) { + Log.d(TAG, "Time out waiting to lock camera closing."); + throw new RuntimeException("Time out waiting to lock camera closing"); + } + Log.d(TAG, "Closing camera: " + mCameraDevices[i].getId()); + mCameraDevices[i].close(); + mCameraDevices[i] = null; + mCameraCaptureSessions[i] = null; + } + if (null != mImageReaders[i]) { + mImageReaders[i].close(); + mImageReaders[i] = null; + } + } + } catch (InterruptedException e) { + mCameraOpenCloseLock.release(); + throw new RuntimeException("Interrupted while trying to lock camera closing.", e); + } catch (IllegalStateException e) { + e.printStackTrace(); + } finally { + mCameraOpenCloseLock.release(); + } + } + + private void openCameraInSequence(String id) { + CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE); + CameraCharacteristics characteristics = null; + try { + characteristics = manager.getCameraCharacteristics(id); + } catch (CameraAccessException e) { + e.printStackTrace(); + } + mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); + Log.d(TAG, "openCameraInSequence " + id + ", mSensorOrientation :" + mSensorOrientation); + try { + if (!mCameraOpenCloseLock.tryAcquire(5000, TimeUnit.MILLISECONDS)) { + Log.d(TAG, "Time out waiting to lock camera opening."); + throw new RuntimeException("Time out waiting to lock camera opening"); + } + manager.openCamera(id, mStateCallback, mMultiCameraModule.getMyCameraHandler()); + } catch (CameraAccessException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + manager = null; + characteristics = null; + } + + private void startBackgroundThread() { + if (mCameraThread == null) { + mCameraThread = new HandlerThread("CameraBackground"); + mCameraThread.start(); + } + if (mCameraHandler == null) { + mCameraHandler = new MyCameraHandler(mCameraThread.getLooper()); + } + } + + private void stopBackgroundThread() { + mCameraThread.quitSafely(); + try { + mCameraThread.join(); + mCameraThread = null; + mCameraHandler = null; + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + private Size[] getSupportedOutputSize(int cameraId, Class cl) { + StreamConfigurationMap map = mCharacteristics.get(cameraId).get( + CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); + Size[] normal = map.getOutputSizes(cl); + Size[] high = map.getHighResolutionOutputSizes(ImageFormat.PRIVATE); + Size[] ret = new Size[normal.length + high.length]; + System.arraycopy(normal, 0, ret, 0, normal.length); + System.arraycopy(high, 0, ret, normal.length, high.length); + return ret; + } + + private Size getOptimalPreviewSize(int id, Size pictureSize, Size[] prevSizes) { + Point[] points = new Point[prevSizes.length]; + + double targetRatio = (double) pictureSize.getWidth() / pictureSize.getHeight(); + int index = 0; + int point_max[] = mMaxPreviewSize[id]; + int max_size = -1; + if (point_max != null){ + max_size = point_max[0] * point_max[1]; + } + for (Size s : prevSizes) { + if (max_size != -1){ + int size = s.getWidth() * s.getHeight(); + if (s.getWidth() == s.getHeight()){ + if (s.getWidth() > Math.max(point_max[0],point_max[1])) + continue; + } else if (size > max_size || size == 0) { + continue; + } + } + points[index++] = new Point(s.getWidth(), s.getHeight()); + } + + int optimalPickIndex = CameraUtil.getOptimalPreviewSize(mActivity, points, targetRatio); + return (optimalPickIndex == -1) ? null : + new Size(points[optimalPickIndex].x,points[optimalPickIndex].y); + } + + private class MyCameraHandler extends Handler { + + public MyCameraHandler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case WAIT_SURFACE: + int id = msg.arg1; + int index = mCameraIDList.indexOf(String.valueOf(id)); + Log.v(TAG, "WAIT_SURFACE id :" + id + ", index :" + index); + if (index == -1) { + break; + } + Surface surface = mMultiCameraUI.getSurfaceViewList().get(index) + .getHolder().getSurface(); + if (surface.isValid()) { + createCaptureSessions(id, surface); + } else { + mCameraHandler.sendMessageDelayed(msg, 200); + Log.v(TAG, "Surface is invalid, wait more 200ms surfaceCreated"); + } + break; + case OPEN_CAMERA: + if (mCameraListIndex == mCameraIDList.size()) { + mCameraListIndex = 0; + } else { + String cameraId = mCameraIDList.get(mCameraListIndex); + openCameraInSequence(cameraId); + mCameraListIndex ++; + Log.v(TAG, " OPEN_CAMERA cameraId :" + cameraId + ", mCameraListIndex :" + + mCameraListIndex); + } + break; + } + } + } + + private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() { + + @Override + public void onOpened(CameraDevice cameraDevice) { + final int id = Integer.parseInt(cameraDevice.getId()); + mCameraDevices[id] = cameraDevice; + Log.d(TAG, "onOpened " + id); + mCameraOpenCloseLock.release(); + //updatePictureSize(id); + createCameraPreviewSession(id); + mActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + mMultiCameraUI.onCameraOpened(id); + } + }); + } + + @Override + public void onDisconnected(CameraDevice cameraDevice) { + int id = Integer.parseInt(cameraDevice.getId()); + Log.d(TAG, "onDisconnected " + id); + mCameraOpenCloseLock.release(); + mCameraDevices[id] = null; + } + + @Override + public void onError(CameraDevice cameraDevice, int error) { + int id = Integer.parseInt(cameraDevice.getId()); + Log.e(TAG, "onError " + id + " " + error); + mCameraOpenCloseLock.release(); + + if (null != mActivity) { + Toast.makeText(mActivity,"open camera error id =" + id, + Toast.LENGTH_LONG).show(); + mActivity.finish(); + } + } + + @Override + public void onClosed(CameraDevice cameraDevice) { + int id = Integer.parseInt(cameraDevice.getId()); + Log.d(TAG, "onClosed " + id); + mCameraOpenCloseLock.release(); + mCameraDevices[id] = null; + } + }; + + /** + * Creates a new {@link CameraCaptureSession} for camera preview. + */ + private void createCameraPreviewSession(int id) { + // This is the output Surface we need to start preview. + int index = mCameraIDList.indexOf(String.valueOf(id)); + Log.v(TAG, "createCameraPreviewSession id :" + id + ", index :" + index); + Surface surface = mMultiCameraUI.getSurfaceViewList().get(index).getHolder().getSurface(); + if (surface.isValid()) { + createCaptureSessions(id, surface); + } else { + Message msg = Message.obtain(); + msg.what = WAIT_SURFACE; + msg.arg1 = id; + mCameraHandler.sendMessageDelayed(msg, 200); + Log.v(TAG, "Surface is invalid, wait 200ms surfaceCreated"); + } + } + + private void createCaptureSessions(final int id, Surface surface) { + try { + // We set up a CaptureRequest.Builder with the output Surface. + mPreviewRequestBuilders[id] + = mCameraDevices[id].createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); + mPreviewRequestBuilders[id].addTarget(surface); + mPreviewRequestBuilders[id].setTag(id); + CameraCaptureSession.StateCallback stateCallback = + new CameraCaptureSession.StateCallback() { + @Override + public void onConfigured(CameraCaptureSession cameraCaptureSession) { + // The camera is already closed + if (null == mCameraDevices[id]) { + return; + } + Log.v(TAG, " CameraCaptureSession onConfigured id :" + id); + // When the session is ready, we start displaying the preview. + mCameraCaptureSessions[id] = cameraCaptureSession; + try { + // Auto focus should be continuous for camera preview. + mPreviewRequestBuilders[id].set(CaptureRequest.CONTROL_AF_MODE, + CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); + // Finally, we start displaying the camera preview. + mCameraCaptureSessions[id].setRepeatingRequest( + mPreviewRequestBuilders[id].build(), + mCaptureCallback, mMultiCameraModule.getMyCameraHandler()); + } catch (CameraAccessException e) { + e.printStackTrace(); + } + + Message msg = Message.obtain(); + msg.what = OPEN_CAMERA; + if (mCameraHandler != null) { + mCameraHandler.sendMessage(msg); + } + } + + @Override + public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { + showToast("onConfigureFailed"); + } + }; + + try { + final byte enable = 1; + mPreviewRequestBuilders[id].set(override_resource_cost_validation, enable); + Log.v(TAG, " set" + override_resource_cost_validation + " is 1"); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + + List<OutputConfiguration> outConfigurations = new ArrayList<>(2); + outConfigurations.add(new OutputConfiguration(surface)); + outConfigurations.add(new OutputConfiguration(mImageReaders[id].getSurface())); + + SessionConfiguration sessionConfiguration = new SessionConfiguration( + SessionConfiguration.SESSION_REGULAR, outConfigurations, + new HandlerExecutor(mCameraHandler), stateCallback); + sessionConfiguration.setSessionParameters(mPreviewRequestBuilders[id].build()); + mCameraDevices[id].createCaptureSession(sessionConfiguration); + try { + CaptureRequest captureRequest = sessionConfiguration.getSessionParameters(); + Log.v(TAG, " override_resource_cost_validation result: " + + captureRequest.get(override_resource_cost_validation)); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } catch (CameraAccessException e) { + e.printStackTrace(); + } + } + + private class HandlerExecutor implements Executor { + private final Handler ihandler; + + public HandlerExecutor(Handler handler) { + ihandler = handler; + } + + @Override + public void execute(Runnable runCmd) { + ihandler.post(runCmd); + } + } + + /** + * A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture. + */ + private CameraCaptureSession.CaptureCallback mCaptureCallback + = new CameraCaptureSession.CaptureCallback() { + + private void process(CaptureResult result) { + Integer afState = result.get(CaptureResult.CONTROL_AF_STATE); + Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE); + if (DEBUG) { + Log.v(TAG, "process afState :" + afState + ", aeState :" + aeState); + } + } + + @Override + public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, + CaptureResult partialResult) { + process(partialResult); + } + + @Override + public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, + TotalCaptureResult result) { + process(result); + } + + }; + + private void updatePictureSize(int id) { + String defaultSize = mActivity.getString(R.string.pref_multi_camera_picturesize_default); + int index = mCameraIDList.indexOf(String.valueOf(id)); + String pictureSize = mLocalSharedPref.getString( + MultiSettingsActivity.KEY_PICTURE_SIZES.get(index), defaultSize); + mPictureSizes[id] = parsePictureSize(pictureSize); + Log.v(TAG, " updatePictureSize size :" + mPictureSizes[id].getWidth() + "x" + + mPictureSizes[id].getHeight()); + mImageReaders[id] = ImageReader.newInstance(mPictureSizes[id].getWidth(), + mPictureSizes[id].getHeight(), ImageFormat.JPEG, /*maxImages*/2); + mImageReaders[id].setOnImageAvailableListener( + mOnImageAvailableListener, mMultiCameraModule.getMyCameraHandler()); + + Size[] prevSizes = getSupportedOutputSize(id, SurfaceHolder.class); + mPreviewSizes[id] = getOptimalPreviewSize(id, mPictureSizes[id], prevSizes); + } + + private final ImageReader.OnImageAvailableListener mOnImageAvailableListener + = new ImageReader.OnImageAvailableListener() { + + @Override + public void onImageAvailable(ImageReader reader) { + Log.v(TAG, "onImageAvailable ..."); + Image image = reader.acquireNextImage(); + mCaptureStartTime = System.currentTimeMillis(); + mNamedImages.nameNewImage(mCaptureStartTime); + NamedEntity name = mNamedImages.getNextNameEntity(); + String title = (name == null) ? null : name.title; + long date = (name == null) ? -1 : name.date; + + byte[] bytes = getJpegData(image); + Log.i(TAG, "image format:" + image.getFormat()); + if (image.getFormat() == ImageFormat.RAW10) { + mActivity.getMediaSaveService().addRawImage(bytes, title, + "raw"); + image.close(); + } else if (image.getFormat() == ImageFormat.YUV_420_888) { + Log.d(TAG, "YUV buffer received" ); + image.close(); + } else { + int orientation = 0; + ExifInterface exif = null; + if (image.getFormat() != ImageFormat.HEIC) { + exif = Exif.getExif(bytes); + orientation = Exif.getOrientation(exif); + } + + String pictureFormat = "jpeg"; + if (image.getFormat() == ImageFormat.HEIC) { + pictureFormat = "heic"; + } + mActivity.getMediaSaveService().addImage(bytes, title, date, + null, image.getWidth(), image.getHeight(), orientation, exif, + mOnMediaSavedListener, mContentResolver, pictureFormat); + + if (image.getFormat() != ImageFormat.HEIC) { + mActivity.updateThumbnail(bytes); + } + image.close(); + mMultiCameraModule.updateTakingPicture(); + } + } + + }; + + /** + * Shows a {@link Toast} on the UI thread. + * @param text The message to show + */ + private void showToast(final String text) { + if (mActivity != null) { + mActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show(); + } + }); + } + } + + private byte[] getJpegData(Image image) { + ByteBuffer buffer = image.getPlanes()[0].getBuffer(); + byte[] bytes = new byte[buffer.remaining()]; + buffer.get(bytes); + return bytes; + } + + @Override + public void onShutterButtonClick(String[] ids) { + checkAndPlayShutterSound(); + mMultiCameraUI.enableShutter(false); + boolean halZSLCheck = mLocalSharedPref.getBoolean(MultiSettingsActivity.KEY_HAL_ZAL, false); + for (String id : ids) { + try { + int cameraId = Integer.parseInt(id); + final CaptureRequest.Builder captureBuilder = + mCameraDevices[cameraId].createCaptureRequest( + CameraDevice.TEMPLATE_STILL_CAPTURE); + captureBuilder.addTarget(mImageReaders[cameraId].getSurface()); + int index = mCameraIDList.indexOf(String.valueOf(cameraId)); + captureBuilder.addTarget(mMultiCameraUI.getSurfaceViewList().get( + index).getHolder().getSurface()); + applySettingsForCapture(captureBuilder, cameraId); + // Use the same AE and AF modes as the preview. + captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, + CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); + if (halZSLCheck) { + captureBuilder.set(CaptureRequest.CONTROL_ENABLE_ZSL, true); + } else { + captureBuilder.set(CaptureRequest.CONTROL_ENABLE_ZSL, false); + } + setAutoFlash(captureBuilder); + // Orientation + int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation(); + captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, + CameraUtil.getJpegRotation(cameraId, rotation)); + mCameraCaptureSessions[cameraId].capture(captureBuilder.build(), + mCaptureStillCallback, mMultiCameraModule.getMyCameraHandler()); + Log.d(TAG, " cameraCaptureSession" + id + " captured "); + } catch (CameraAccessException e) { + e.printStackTrace(); + } + } + } + + @Override + public void onVideoButtonClick(String[] ids) { + + } + + @Override + public void onButtonPause(String[] ids) { + + } + + @Override + public void onButtonContinue(String[] ids) { + + } + + @Override + public void onOrientationChanged(int orientation) { + + } + + @Override + public boolean isRecordingVideo() { + return false; + } + + private void checkAndPlayShutterSound() { + boolean isPlay = mLocalSharedPref.getBoolean(MultiSettingsActivity.KEY_SHUTTER_SOUND, true); + if (mSoundPlayer != null && isPlay) { + mSoundPlayer.play(SoundClips.SHUTTER_CLICK); + } + } + + private MediaSaveService.OnMediaSavedListener mOnMediaSavedListener = + new MediaSaveService.OnMediaSavedListener() { + @Override + public void onMediaSaved(Uri uri) { + if (uri != null) { + mActivity.notifyNewMedia(uri); + } + } + }; + + private CameraCaptureSession.CaptureCallback mCaptureStillCallback + = new CameraCaptureSession.CaptureCallback() { + + @Override + public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, + TotalCaptureResult result) { + Log.d(TAG, " mCaptureCallback onCaptureCompleted "); + mMultiCameraModule.getMainHandler().post(new Runnable() { + @Override + public void run() { + Log.d(TAG, " enable Shutter " ); + mMultiCameraUI.enableShutter(true); + } + }); + } + + @Override + public void onCaptureFailed(CameraCaptureSession session, + CaptureRequest request, + CaptureFailure result) { + Log.d(TAG, " mCaptureCallback onCaptureFailed " ); + } + + + @Override + public void onCaptureSequenceCompleted(CameraCaptureSession session, int + sequenceId, long frameNumber) { + Log.d(TAG, " mCaptureCallback onCaptureSequenceCompleted "); + } + }; + + private void applySettingsForCapture(CaptureRequest.Builder builder, int id) { + builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE); + applyJpegQuality(builder); + } + + private void applyJpegQuality(CaptureRequest.Builder request) { + String defaultValue = mActivity.getString(R.string.pref_camera_jpegquality_default); + String value = mLocalSharedPref.getString(MultiSettingsActivity.KEY_PICTURE_QUALITY, + defaultValue); + int jpegQuality = getQualityNumber(value); + request.set(CaptureRequest.JPEG_QUALITY, (byte) jpegQuality); + } + + private int getQualityNumber(String jpegQuality) { + if (jpegQuality == null) { + return 85; + } + try { + int qualityPercentile = Integer.parseInt(jpegQuality); + if (qualityPercentile >= 0 && qualityPercentile <= 100) + return qualityPercentile; + else + return 85; + } catch (NumberFormatException nfe) { + //chosen quality is not a number, continue + } + int value = 0; + switch (jpegQuality) { + case "superfine": + value = CameraProfile.QUALITY_HIGH; + break; + case "fine": + value = CameraProfile.QUALITY_MEDIUM; + break; + case "normal": + value = CameraProfile.QUALITY_LOW; + break; + default: + return 85; + } + return CameraProfile.getJpegEncodingQualityParameter(value); + } + + private Size parsePictureSize(String value) { + int indexX = value.indexOf('x'); + int width = Integer.parseInt(value.substring(0, indexX)); + int height = Integer.parseInt(value.substring(indexX + 1)); + return new Size(width, height); + } + + private void initCameraCharacteristics() { + mCharacteristics = new ArrayList<>(); + CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE); + try { + String[] cameraIdList = manager.getCameraIdList(); + Log.d(TAG, "cameraIdList size =" + cameraIdList.length); + for (int i = 0; i < cameraIdList.length; i++) { + String cameraId = cameraIdList[i]; + CameraCharacteristics characteristics + = manager.getCameraCharacteristics(cameraId); + mCharacteristics.add(i, characteristics); + try { + mMaxPreviewSize[i] = characteristics.get(CaptureModule.max_preview_size); + } catch (IllegalArgumentException e) { + Log.e(TAG, "getMaxPreviewSize no vendorTag max_preview_size:"); + } + if (mMaxPreviewSize[i] != null) { + Log.d(TAG, " init cameraId :" + cameraId + ", i :" + i + + ", maxPreviewSize :" + mMaxPreviewSize[i][0]+ "x" + + mMaxPreviewSize[i][1]); + } + } + } catch (CameraAccessException e) { + e.printStackTrace(); + } + } + + private void setAutoFlash(CaptureRequest.Builder requestBuilder) { + if (true) { + requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, + CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); + } + } }
\ No newline at end of file diff --git a/src/com/android/camera/multi/MultiSettingsActivity.java b/src/com/android/camera/multi/MultiSettingsActivity.java index a3bd75f80..a3bd75f80 100755..100644 --- a/src/com/android/camera/multi/MultiSettingsActivity.java +++ b/src/com/android/camera/multi/MultiSettingsActivity.java diff --git a/src/com/android/camera/multi/MultiVideoModule.java b/src/com/android/camera/multi/MultiVideoModule.java index b07e7ca18..0caf30d30 100644 --- a/src/com/android/camera/multi/MultiVideoModule.java +++ b/src/com/android/camera/multi/MultiVideoModule.java @@ -1,1332 +1,1338 @@ -/*
- * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
- * Not a Contribution.
- *
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.camera.multi;
-
-import android.content.Context;
-import android.content.ContentResolver;
-import android.content.ContentValues;
-import android.content.SharedPreferences;
-import android.graphics.Bitmap;
-import android.graphics.ImageFormat;
-import android.graphics.Point;
-import android.hardware.camera2.CameraAccessException;
-import android.hardware.camera2.CameraCaptureSession;
-import android.hardware.camera2.CameraCharacteristics;
-import android.hardware.camera2.CameraDevice;
-import android.hardware.camera2.CaptureFailure;
-import android.hardware.camera2.CameraManager;
-import android.hardware.camera2.CameraMetadata;
-import android.hardware.camera2.CaptureRequest;
-import android.hardware.camera2.CaptureResult;
-import android.hardware.camera2.TotalCaptureResult;
-import android.hardware.camera2.params.OutputConfiguration;
-import android.hardware.camera2.params.SessionConfiguration;
-import android.hardware.camera2.params.StreamConfigurationMap;
-import android.location.Location;
-import android.media.AudioManager;
-import android.media.CamcorderProfile;
-import android.media.Image;
-import android.media.ImageReader;
-import android.media.MediaRecorder;
-import android.media.MediaMetadataRetriever;
-import android.net.Uri;
-import android.os.Handler;
-import android.os.HandlerThread;
-import android.os.Looper;
-import android.os.Message;
-import android.os.SystemClock;
-import android.os.ParcelFileDescriptor;
-import android.provider.MediaStore;
-import android.util.Log;
-import android.util.Size;
-import android.view.OrientationEventListener;
-import android.util.SparseIntArray;
-import android.view.Surface;
-import android.view.SurfaceHolder;
-import android.view.WindowManager;
-import android.widget.Toast;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.text.SimpleDateFormat;
-import java.util.Arrays;
-import java.util.ArrayList;
-import java.util.concurrent.Executor;
-import java.util.List;
-import java.util.Date;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.TimeUnit;
-
-import com.android.camera.CameraActivity;
-import com.android.camera.CaptureModule;
-import com.android.camera.CameraSettings;
-import com.android.camera.ComboPreferences;
-import com.android.camera.Exif;
-import com.android.camera.exif.ExifInterface;
-import com.android.camera.LocationManager;
-import com.android.camera.MediaSaveService;
-import com.android.camera.PhotoModule.NamedImages;
-import com.android.camera.PhotoModule.NamedImages.NamedEntity;
-import com.android.camera.SoundClips;
-import com.android.camera.Storage;
-import com.android.camera.Thumbnail;
-import com.android.camera.util.CameraUtil;
-import com.android.camera.util.SettingTranslation;
-import com.android.camera.util.PersistUtil;
-import com.android.camera.ui.RotateTextToast;
-
-import org.codeaurora.snapcam.R;
-
-public class MultiVideoModule implements MultiCamera, LocationManager.Listener,
- MediaRecorder.OnErrorListener, MediaRecorder.OnInfoListener {
-
- private static final String TAG = "SnapCam_MultiVideoModule";
-
- private static final boolean DEBUG =
- (PersistUtil.getCamera2Debug() == PersistUtil.CAMERA2_DEBUG_DUMP_LOG) ||
- (PersistUtil.getCamera2Debug() == PersistUtil.CAMERA2_DEBUG_DUMP_ALL);
-
- private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
- private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
- private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
- private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
-
- private static final int WAIT_SURFACE = 0;
- private static final int OPEN_CAMERA = 1;
-
- private static final int SCREEN_DELAY = 2 * 60 * 1000;
-
- private static final int MAX_NUM_CAM = 16;
-
- private int mCameraListIndex = 0;
-
- private static final CaptureRequest.Key<Byte> override_resource_cost_validation =
- new CaptureRequest.Key<>(
- "org.codeaurora.qcamera3.sessionParameters.overrideResourceCostValidation",
- byte.class);
-
- private CameraActivity mActivity;
- private MultiCameraUI mMultiCameraUI;
- private MultiCameraModule mMultiCameraModule;
- private SharedPreferences mLocalSharedPref;
- private ArrayList<CameraCharacteristics> mCharacteristics;
- private CameraDevice[] mCameraDevices = new CameraDevice[MAX_NUM_CAM];
- private CameraCaptureSession[] mCameraPreviewSessions = new CameraCaptureSession[MAX_NUM_CAM];
- private ContentValues[] mCurrentVideoValues = new ContentValues[MAX_NUM_CAM];
- private ImageReader[] mImageReaders = new ImageReader[MAX_NUM_CAM];
- private MediaRecorder[] mMediaRecorders = new MediaRecorder[MAX_NUM_CAM];
- private String[] mNextVideoAbsolutePaths = new String[MAX_NUM_CAM];
- private boolean mPaused = true;
-
- private NamedImages mNamedImages;
-
- private Uri mCurrentVideoUri;
-
- private boolean mMediaRecorderPausing = false;
-
- private boolean mRecordingTimeCountsDown = false;
-
- private LocationManager mLocationManager;
- private CamcorderProfile mProfile;
-
- private Size[] mVideoSize = new Size[MAX_NUM_CAM];
- private Size mPreviewSizes[] = new Size[MAX_NUM_CAM];
- private int[][] mMaxPreviewSize = new int[MAX_NUM_CAM][];
-
- private boolean mCaptureTimeLapse = false;
- // Default 0. If it is larger than 0, the camcorder is in time lapse mode.
- private int mTimeBetweenTimeLapseFrameCaptureMs = 0;
-
- private String[] mVideoFilenames = new String[MAX_NUM_CAM];
-
- private long mRecordingStartTime;
- private long mRecordingTotalTime;
-
- private ParcelFileDescriptor mVideoFileDescriptor;
-
- // The video duration limit. 0 means no limit.
- private int mMaxVideoDurationInMs;
-
- private int mAudioEncoder;
- private String mVideoRotation;
-
- private SoundClips.Player mSoundPlayer;
-
- /**
- * Whether the app is recording video now
- */
- private boolean[] mIsRecordingVideos = new boolean[MAX_NUM_CAM];
-
- private String[] mCameraIds;
- private ArrayList<String> mCameraIDList = new ArrayList<>();
-
- /**
- * Orientation of the camera sensor
- */
- private int mSensorOrientation;
- private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
-
- /**
- * {@link CaptureRequest.Builder} for the camera preview
- */
- private CaptureRequest.Builder[] mPreviewRequestBuilders = new CaptureRequest.Builder[MAX_NUM_CAM];
-
- private Handler mCameraHandler;
- private HandlerThread mCameraThread;
-
- private ContentResolver mContentResolver;
- /**
- * A {@link Semaphore} make sure the camera open callback happens first before closing the
- * camera.
- */
- private Semaphore mCameraOpenCloseLock = new Semaphore(3);
-
- public MultiVideoModule(CameraActivity activity, MultiCameraUI ui, MultiCameraModule module) {
- mActivity = activity;
- mMultiCameraUI = ui;
- mMultiCameraModule = module;
- mContentResolver = mActivity.getContentResolver();
- mLocationManager = new LocationManager(mActivity, this);
- mNamedImages = new NamedImages();
- mLocalSharedPref = mActivity.getSharedPreferences(
- ComboPreferences.getLocalSharedPreferencesName(mActivity,
- "multi" + mMultiCameraModule.getCurrenCameraMode()), Context.MODE_PRIVATE);
- startBackgroundThread();
- initCameraCharacteristics();
- }
-
- @Override
- public void onResume(String[] ids) {
- for (String id : ids) {
- mCameraIDList.add(id);
- }
- // Set up sound playback for video record and video stop
- if (mSoundPlayer == null) {
- mSoundPlayer = SoundClips.getPlayer(mActivity);
- }
- mPaused = false;
- initializeValues();
- startBackgroundThread();
- for (String id : ids) {
- int cameraId = Integer.parseInt(id);
- updateVideoSize(cameraId);
- int index = mCameraIDList.indexOf(id);
- mMultiCameraUI.setPreviewSize(index, mPreviewSizes[cameraId].getWidth(),
- mPreviewSizes[cameraId].getHeight());
- }
- }
-
- @Override
- public void onPause() {
- mPaused = true;
- if (mSoundPlayer != null) {
- mSoundPlayer.release();
- mSoundPlayer = null;
- }
- for (String id : mCameraIds) {
- int cameraId = Integer.parseInt(id);
- Log.d(TAG, " onPause id :" + cameraId + " recording is :" +
- (mIsRecordingVideos[cameraId] ? "STOPED" : "START"));
- if (mIsRecordingVideos[cameraId]) {
- stopRecordingVideo(cameraId);
- }
- }
-
- if (mCameraIDList != null) {
- mCameraIDList.clear();
- }
- mCameraListIndex = 0;
- closeCamera();
- stopBackgroundThread();
- }
-
- @Override
- public boolean openCamera(String[] ids) {
- mCameraIds = ids;
- for (String id : ids) {
- mCameraIDList.add(id);
- }
- Message msg = Message.obtain();
- msg.what = OPEN_CAMERA;
- if (mCameraHandler != null) {
- mCameraHandler.sendMessage(msg);
- }
- return true;
- }
-
- @Override
- public void startPreview() {
-
- }
-
- @Override
- public void closeSession() {
-
- }
-
- @Override
- public void closeCamera() {
- Log.d(TAG, "closeCamera");
- /* no need to set this in the callback and handle asynchronously. This is the same
- reason as why we release the semaphore here, not in camera close callback function
- as we don't have to protect the case where camera open() gets called during camera
- close(). The low level framework/HAL handles the synchronization for open()
- happens after close() */
- try {
- // Close camera starting with AUX first
- for (int i = MAX_NUM_CAM - 1; i >= 0; i--) {
- if (null != mCameraDevices[i]) {
- if (!mCameraOpenCloseLock.tryAcquire(2000, TimeUnit.MILLISECONDS)) {
- Log.d(TAG, "Time out waiting to lock camera closing.");
- throw new RuntimeException("Time out waiting to lock camera closing");
- }
- Log.d(TAG, "Closing camera: " + mCameraDevices[i].getId());
- mCameraDevices[i].close();
- mCameraDevices[i] = null;
- mCameraPreviewSessions[i] = null;
- }
- if (null != mImageReaders[i]) {
- mImageReaders[i].close();
- mImageReaders[i] = null;
- }
- }
- } catch (InterruptedException e) {
- mCameraOpenCloseLock.release();
- throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } finally {
- mCameraOpenCloseLock.release();
- }
- }
-
- @Override
- public void onVideoButtonClick(String[] ids) {
- checkAndPlayShutterSound(mIsRecordingVideos[0]);
- for (String id : ids) {
- int cameraId = Integer.parseInt(id);
- Log.d(TAG, " onVideoButtonClick id :" + cameraId + " recording is :" +
- (mIsRecordingVideos[cameraId] ? "STOPED" : "START"));
- if (mIsRecordingVideos[cameraId]) {
- stopRecordingVideo(cameraId);
- } else {
- startRecordingVideo(cameraId);
- }
- }
- }
-
- @Override
- public void onShutterButtonClick(String[] ids) {
- checkAndPlayCaptureSound();
- mMultiCameraUI.enableShutter(false);
- for (String id : ids) {
- Log.d(TAG, "onShutterButtonClick id :" + id);
- try {
- int cameraId = Integer.parseInt(id);
- if (null == mActivity || null == mCameraDevices[cameraId]) {
- warningToast("Camera is not ready yet to take a video snapshot.");
- return;
- }
- final CaptureRequest.Builder captureBuilder =
- mCameraDevices[cameraId].createCaptureRequest(
- CameraDevice.TEMPLATE_VIDEO_SNAPSHOT);
- captureBuilder.addTarget(mImageReaders[cameraId].getSurface());
- int index = mCameraIDList.indexOf(String.valueOf(cameraId));
- captureBuilder.addTarget(mMultiCameraUI.getSurfaceViewList().get(
- index).getHolder().getSurface());
- // Use the same AE and AF modes as the preview.
- captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
- CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
- captureBuilder.set(CaptureRequest.JPEG_THUMBNAIL_QUALITY, (byte) 80);
- captureBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);
- captureBuilder.set(CaptureRequest.CONTROL_AE_MODE,
- CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
-
- // Orientation
- int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
- captureBuilder.set(CaptureRequest.JPEG_ORIENTATION,
- CameraUtil.getJpegRotation(cameraId, rotation));
- mCameraPreviewSessions[cameraId].capture(captureBuilder.build(),
- mCaptureStillCallback, mMultiCameraModule.getMyCameraHandler());
- Log.d(TAG, " cameraCaptureSession" + id + " captured ");
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
- }
-
- @Override
- public void onButtonPause(String[] ids) {
- mRecordingTotalTime += SystemClock.uptimeMillis() - mRecordingStartTime;
- mMediaRecorderPausing = true;
- for (String id : ids) {
- int cameraId = Integer.parseInt(id);
- mMediaRecorders[cameraId].pause();
- }
- }
-
- @Override
- public void onButtonContinue(String[] ids) {
- mMediaRecorderPausing = false;
- for (String id : ids) {
- int cameraId = Integer.parseInt(id);
- mMediaRecorders[cameraId].resume();
- mRecordingStartTime = SystemClock.uptimeMillis();
- updateRecordingTime(cameraId);
- }
- }
-
- @Override
- public void onErrorListener(int error) {
- enableRecordingLocation(false);
- }
-
- // from MediaRecorder.OnErrorListener
- @Override
- public void onError(MediaRecorder mr, int what, int extra) {
- Log.e(TAG, "MediaRecorder error. what=" + what + ". extra=" + extra);
- String[] ids = {"0", "1"};
- for (String id : ids) {
- int cameraId = Integer.parseInt(id);
- stopRecordingVideo(cameraId);
- }
- if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) {
- // We may have run out of space on the sdcard.
- mActivity.updateStorageSpaceAndHint();
- } else {
- warningToast("MediaRecorder error. what=" + what + ". extra=" + extra);
- }
- }
-
- // from MediaRecorder.OnInfoListener
- @Override
- public void onInfo(MediaRecorder mr, int what, int extra) {
- String[] ids = {"0", "1"};
- if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
- for (String id : ids) {
- int cameraId = Integer.parseInt(id);
- if (mIsRecordingVideos[cameraId]) {
- stopRecordingVideo(cameraId);
- }
- }
- } else if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
- for (String id : ids) {
- int cameraId = Integer.parseInt(id);
- if (mIsRecordingVideos[cameraId]) {
- stopRecordingVideo(cameraId);
- }
- }
- // Show the toast.
- RotateTextToast.makeText(mActivity, R.string.video_reach_size_limit,
- Toast.LENGTH_LONG).show();
- }
- }
-
- @Override
- public void onOrientationChanged(int orientation) {
- mOrientation = orientation;
- }
-
- @Override
- public boolean isRecordingVideo() {
- for (int i = 0; i < mIsRecordingVideos.length; i++) {
- if (mIsRecordingVideos[i]) return true;
- }
- return false;
- }
-
- private void openCameraInSequence(String id) {
- CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
- CameraCharacteristics characteristics = null;
- try {
- characteristics = manager.getCameraCharacteristics(id);
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
- Log.d(TAG, "openCameraInSequence " + id + ", mSensorOrientation :" + mSensorOrientation);
- try {
- if (!mCameraOpenCloseLock.tryAcquire(5000, TimeUnit.MILLISECONDS)) {
- Log.d(TAG, "Time out waiting to lock camera opening.");
- throw new RuntimeException("Time out waiting to lock camera opening");
- }
- manager.openCamera(id, mStateCallback, mMultiCameraModule.getMyCameraHandler());
- } catch (CameraAccessException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- manager = null;
- characteristics = null;
- }
-
- private void initCameraCharacteristics() {
- mCharacteristics = new ArrayList<>();
- CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
- try {
- String[] cameraIdList = manager.getCameraIdList();
- Log.d(TAG, "cameraIdList size =" + cameraIdList.length);
- for (int i = 0; i < cameraIdList.length; i++) {
- String cameraId = cameraIdList[i];
- CameraCharacteristics characteristics
- = manager.getCameraCharacteristics(cameraId);
- mCharacteristics.add(i, characteristics);
- try {
- mMaxPreviewSize[i] = characteristics.get(CaptureModule.max_preview_size);
- } catch (IllegalArgumentException e) {
- Log.e(TAG, "getMaxPreviewSize no vendorTag max_preview_size:");
- }
- if (mMaxPreviewSize[i] != null) {
- Log.d(TAG, " init cameraId :" + cameraId + ", i :" + i +
- ", maxPreviewSize :" + mMaxPreviewSize[i][0]+ "x" +
- mMaxPreviewSize[i][1]);
- }
- }
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
-
- private void startBackgroundThread() {
- if (mCameraThread == null) {
- mCameraThread = new HandlerThread("CameraBackground");
- mCameraThread.start();
- }
- if (mCameraHandler == null) {
- mCameraHandler = new MyCameraHandler(mCameraThread.getLooper());
- }
- }
-
- private void stopBackgroundThread() {
- mCameraThread.quitSafely();
- try {
- mCameraThread.join();
- mCameraThread = null;
- mCameraHandler = null;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- private class MyCameraHandler extends Handler {
-
- public MyCameraHandler(Looper looper) {
- super(looper);
- }
-
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case WAIT_SURFACE:
- int id = msg.arg1;
- int index = mCameraIDList.indexOf(String.valueOf(id));
- Log.v(TAG, "WAIT_SURFACE id :" + id + ", index :" + index);
- if (index == -1) {
- break;
- }
- Surface surface = mMultiCameraUI.getSurfaceViewList().get(index)
- .getHolder().getSurface();
- if (surface.isValid()) {
- createCaptureSessions(id, surface);
- } else {
- mCameraHandler.sendMessageDelayed(msg, 200);
- Log.v(TAG, "Surface is invalid, wait more 200ms surfaceCreated");
- }
- break;
- case OPEN_CAMERA:
- if (mCameraListIndex == mCameraIDList.size()) {
- mCameraListIndex = 0;
- } else {
- String cameraId = mCameraIDList.get(mCameraListIndex);
- openCameraInSequence(cameraId);
- mCameraListIndex ++;
- Log.v(TAG, " OPEN_CAMERA cameraId :" + cameraId + ", mCameraListIndex :"
- + mCameraListIndex);
- }
- break;
- }
- }
- }
-
- private void createVideoSnapshotImageReader(int id) {
- if (mImageReaders[id] != null) {
- mImageReaders[id].close();
- }
- mImageReaders[id] = ImageReader.newInstance(mVideoSize[id].getWidth(),
- mVideoSize[id].getHeight(),
- ImageFormat.JPEG, /*maxImages*/2);
- mImageReaders[id].setOnImageAvailableListener(
- mOnImageAvailableListener, mMultiCameraModule.getMyCameraHandler());
- }
-
- private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
-
- @Override
- public void onOpened(CameraDevice cameraDevice) {
- int id = Integer.parseInt(cameraDevice.getId());
- mCameraDevices[id] = cameraDevice;
- Log.d(TAG, "onOpened " + id);
- mCameraOpenCloseLock.release();
- createCameraPreviewSession(id);
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mMultiCameraUI.onCameraOpened(id);
- }
- });
- }
-
- @Override
- public void onDisconnected(CameraDevice cameraDevice) {
- int id = Integer.parseInt(cameraDevice.getId());
- Log.d(TAG, "onDisconnected " + id);
- mCameraOpenCloseLock.release();
- mCameraDevices[id] = null;
- }
-
- @Override
- public void onError(CameraDevice cameraDevice, int error) {
- int id = Integer.parseInt(cameraDevice.getId());
- Log.e(TAG, "onError " + id + " " + error);
- mCameraOpenCloseLock.release();
-
- if (null != mActivity) {
- Toast.makeText(mActivity,"open camera error id =" + id,
- Toast.LENGTH_LONG).show();
- mActivity.finish();
- }
- }
-
- @Override
- public void onClosed(CameraDevice cameraDevice) {
- int id = Integer.parseInt(cameraDevice.getId());
- Log.d(TAG, "onClosed " + id);
- mCameraOpenCloseLock.release();
- mCameraDevices[id] = null;
- }
- };
-
- private CameraCaptureSession.CaptureCallback mCaptureStillCallback
- = new CameraCaptureSession.CaptureCallback() {
-
- @Override
- public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
- TotalCaptureResult result) {
- Log.d(TAG, " mCaptureCallback onCaptureCompleted ");
- mMultiCameraModule.getMainHandler().post(new Runnable() {
- @Override
- public void run() {
- Log.d(TAG, " enable Shutter " );
- mMultiCameraUI.enableShutter(true);
- }
- });
- }
-
- @Override
- public void onCaptureFailed(CameraCaptureSession session,
- CaptureRequest request,
- CaptureFailure result) {
- Log.d(TAG, " mCaptureCallback onCaptureFailed " );
- }
-
-
- @Override
- public void onCaptureSequenceCompleted(CameraCaptureSession session, int
- sequenceId, long frameNumber) {
- Log.d(TAG, " mCaptureCallback onCaptureSequenceCompleted ");
- }
- };
-
- private MediaSaveService.OnMediaSavedListener mOnMediaSavedListener =
- new MediaSaveService.OnMediaSavedListener() {
- @Override
- public void onMediaSaved(Uri uri) {
- if (uri != null) {
- mActivity.notifyNewMedia(uri);
- }
- }
- };
-
- public void enableRecordingLocation(boolean enable) {
- mLocationManager.recordLocation(enable);
- }
-
- private Size parsePictureSize(String value) {
- int indexX = value.indexOf('x');
- int width = Integer.parseInt(value.substring(0, indexX));
- int height = Integer.parseInt(value.substring(indexX + 1));
- return new Size(width, height);
- }
-
- private void updateVideoSize(int id) {
- String defaultSize = mActivity.getString(R.string.pref_multi_camera_video_quality_default);
- int index = mCameraIDList.indexOf(String.valueOf(id));
- String videoSize = mLocalSharedPref.getString(
- MultiSettingsActivity.KEY_VIDEO_SIZES.get(index), defaultSize);
- Size size = parsePictureSize(videoSize);
- mVideoSize[id] = size;
- Log.v(TAG, " updateVideoSize size :" + mVideoSize[id].getWidth() + "x" +
- mVideoSize[id].getHeight());
-
- Size[] prevSizes = getSupportedOutputSize(id, MediaRecorder.class);
- mPreviewSizes[id] = getOptimalVideoPreviewSize(id, mVideoSize[id], prevSizes);
- }
-
- private Size[] getSupportedOutputSize(int cameraId, Class cl) {
- StreamConfigurationMap map = mCharacteristics.get(cameraId).get(
- CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
- Size[] normal = map.getOutputSizes(cl);
- Size[] high = map.getHighResolutionOutputSizes(ImageFormat.PRIVATE);
- Size[] ret = new Size[normal.length + high.length];
- System.arraycopy(normal, 0, ret, 0, normal.length);
- System.arraycopy(high, 0, ret, normal.length, high.length);
- return ret;
- }
-
- private Size getOptimalVideoPreviewSize(int id, Size VideoSize, Size[] prevSizes) {
- Point[] points = new Point[prevSizes.length];
-
- int index = 0;
- int point_max[] = mMaxPreviewSize[id];
- int max_size = -1;
- if (point_max != null) {
- max_size = point_max[0] * point_max[1];
- }
- for (Size s : prevSizes) {
- if (max_size != -1) {
- int size = s.getWidth() * s.getHeight();
- if (s.getWidth() == s.getHeight()) {
- if (s.getWidth() > Math.max(point_max[0], point_max[1]))
- continue;
- } else if (size > max_size || size == 0) {
- continue;
- }
- }
- points[index++] = new Point(s.getWidth(), s.getHeight());
- }
-
- int optimalPickIndex = CameraUtil.getOptimalVideoPreviewSize(mActivity, points, VideoSize);
- return (optimalPickIndex == -1) ? null :
- new Size(points[optimalPickIndex].x, points[optimalPickIndex].y);
- }
-
- /**
- * Creates a new {@link CameraCaptureSession} for camera preview.
- */
- private void createCameraPreviewSession(int id) {
- // This is the output Surface we need to start preview.
- int index = mCameraIDList.indexOf(String.valueOf(id));
- Log.v(TAG, "createCameraPreviewSession id :" + id + ", index :" + index);
- Surface surface = mMultiCameraUI.getSurfaceViewList().get(index).getHolder().getSurface();
-
- if (surface.isValid()) {
- createCaptureSessions(id, surface);
- } else {
- Message msg = Message.obtain();
- msg.what = WAIT_SURFACE;
- msg.arg1 = id;
- mCameraHandler.sendMessageDelayed(msg, 200);
- Log.v(TAG, "Surface is invalid, wait 200ms surfaceCreated");
- }
-
- }
-
- private void createCaptureSessions(int id, Surface surface) {
- try {
- // We set up a CaptureRequest.Builder with the output Surface.
- mPreviewRequestBuilders[id]
- = mCameraDevices[id].createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
- mPreviewRequestBuilders[id].addTarget(surface);
- mPreviewRequestBuilders[id].setTag(id);
-
- CameraCaptureSession.StateCallback stateCallback =
- new CameraCaptureSession.StateCallback() {
- @Override
- public void onConfigured(CameraCaptureSession cameraCaptureSession) {
- // The camera is already closed
- if (null == mCameraDevices[id]) {
- return;
- }
- Log.v(TAG, " CameraCaptureSession onConfigured id :" + id);
- // When the session is ready, we start displaying the preview.
- mCameraPreviewSessions[id] = cameraCaptureSession;
- try {
- // Auto focus should be continuous for camera preview.
- mPreviewRequestBuilders[id].set(CaptureRequest.CONTROL_AF_MODE,
- CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
- // Finally, we start displaying the camera preview.
- mCameraPreviewSessions[id].setRepeatingRequest(
- mPreviewRequestBuilders[id].build(),
- mCaptureCallback, mMultiCameraModule.getMyCameraHandler());
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
-
- if (mCameraDevices[mCameraListIndex] == null) {
- Message msg = Message.obtain();
- msg.what = OPEN_CAMERA;
- if (mCameraHandler != null) {
- mCameraHandler.sendMessage(msg);
- }
- }
- }
-
- @Override
- public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
- showToast("onConfigureFailed");
- }
- };
-
- try {
- final byte enable = 1;
- mPreviewRequestBuilders[id].set(override_resource_cost_validation, enable);
- Log.v(TAG, " set" + override_resource_cost_validation + " is 1");
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
-
- List<OutputConfiguration> outConfigurations = new ArrayList<>(1);
- outConfigurations.add(new OutputConfiguration(surface));
-
- SessionConfiguration sessionConfiguration = new SessionConfiguration(
- SessionConfiguration.SESSION_REGULAR, outConfigurations,
- new HandlerExecutor(mCameraHandler), stateCallback);
- sessionConfiguration.setSessionParameters(mPreviewRequestBuilders[id].build());
- mCameraDevices[id].createCaptureSession(sessionConfiguration);
-
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
-
- private class HandlerExecutor implements Executor {
- private final Handler ihandler;
-
- public HandlerExecutor(Handler handler) {
- ihandler = handler;
- }
-
- @Override
- public void execute(Runnable runCmd) {
- ihandler.post(runCmd);
- }
- }
-
- /**
- * A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture.
- */
- private CameraCaptureSession.CaptureCallback mCaptureCallback
- = new CameraCaptureSession.CaptureCallback() {
-
- private void process(CaptureResult result) {
- Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
- Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
- if (DEBUG) {
- Log.v(TAG, "process afState :" + afState + ", aeState :" + aeState);
- }
- }
-
- @Override
- public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
- CaptureResult partialResult) {
- process(partialResult);
- }
-
- @Override
- public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
- TotalCaptureResult result) {
- process(result);
- }
-
- };
-
- private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
- = new ImageReader.OnImageAvailableListener() {
-
- @Override
- public void onImageAvailable(ImageReader reader) {
- Log.v(TAG, " onImageAvailable ...");
- Image image = reader.acquireNextImage();
- long imageTime = System.currentTimeMillis();
- mNamedImages.nameNewImage(imageTime);
- NamedEntity name = mNamedImages.getNextNameEntity();
- String title = (name == null) ? null : name.title;
- long date = (name == null) ? -1 : name.date;
-
- ByteBuffer buffer = image.getPlanes()[0].getBuffer();
- byte[] bytes = new byte[buffer.remaining()];
- buffer.get(bytes);
-
- int orientation = 0;
- ExifInterface exif = Exif.getExif(bytes);
- orientation = Exif.getOrientation(exif);
- String saveFormat = "jpeg";
- mActivity.getMediaSaveService().addImage(bytes, title, date,
- null, image.getWidth(), image.getHeight(), orientation, exif,
- mOnMediaSavedListener, mContentResolver, saveFormat);
- mActivity.updateThumbnail(bytes);
- image.close();
- mMultiCameraModule.updateTakingPicture();
- }
- };
-
- private void initializeValues() {
- updateAudioEncoder();
- updateVideoRotation();
- }
-
- private void checkAndPlayShutterSound(boolean isStarted) {
- if (mSoundPlayer != null) {
- mSoundPlayer.play(isStarted? SoundClips.STOP_VIDEO_RECORDING
- : SoundClips.START_VIDEO_RECORDING);
- }
- }
-
- private void checkAndPlayCaptureSound() {
- if (mSoundPlayer != null) {
- mSoundPlayer.play(SoundClips.SHUTTER_CLICK);
- }
- }
-
- private void closePreviewSession(int id) {
- if (mCameraPreviewSessions[id] != null) {
- Log.v(TAG, "closePreviewSession id :" + id);
- mCameraPreviewSessions[id].close();
- mCameraPreviewSessions[id] = null;
- }
- }
-
- private void startRecordingVideo(final int id) {
- int index = mCameraIDList.indexOf(String.valueOf(id));
- if (null == mCameraDevices[id] ||
- !mMultiCameraUI.getSurfaceViewList().get(index).isEnabled()) {
- return;
- }
- Log.v(TAG, " startRecordingVideo " + id);
- try {
- closePreviewSession(id);
- setUpMediaRecorder(id);
- createVideoSnapshotImageReader(id);
- mPreviewRequestBuilders[id] = mCameraDevices[id].createCaptureRequest(
- CameraDevice.TEMPLATE_RECORD);
- if (true) {
- mPreviewRequestBuilders[id].set(CaptureRequest.NOISE_REDUCTION_MODE,
- CaptureRequest.NOISE_REDUCTION_MODE_FAST);
- } else {
- mPreviewRequestBuilders[id].set(CaptureRequest.NOISE_REDUCTION_MODE,
- CaptureRequest.NOISE_REDUCTION_MODE_HIGH_QUALITY);
- }
- List<Surface> surfaces = new ArrayList<>();
-
- // Set up Surface for the camera preview
- Surface previewSurface = mMultiCameraUI.getSurfaceViewList().get(index).getHolder()
- .getSurface();
- surfaces.add(previewSurface);
- mPreviewRequestBuilders[id].addTarget(previewSurface);
-
- // Set up Surface for the MediaRecorder
- Surface recorderSurface = mMediaRecorders[id].getSurface();
- surfaces.add(recorderSurface);
- mPreviewRequestBuilders[id].addTarget(recorderSurface);
- surfaces.add(mImageReaders[id].getSurface());
-
- // Start a capture session
- // Once the session starts, we can update the UI and start recording
- mCameraDevices[id].createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
-
- @Override
- public void onConfigured(CameraCaptureSession cameraCaptureSession) {
- mCameraPreviewSessions[id] = cameraCaptureSession;
- updatePreview(id);
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mIsRecordingVideos[id] = true;
- // Start recording
- mMediaRecorders[id].start();
- requestAudioFocus();
- mRecordingTotalTime = 0L;
- mRecordingStartTime = SystemClock.uptimeMillis();
- mMediaRecorderPausing = false;
- mMultiCameraUI.resetPauseButton();
- mMultiCameraUI.showRecordingUI(true);
- updateRecordingTime(id);
- keepScreenOn();
- Log.v(TAG, " startRecordingVideo done " + id);
- }
- });
- }
-
- @Override
- public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
- if (null != mActivity) {
- Toast.makeText(mActivity, "Configure Failed", Toast.LENGTH_SHORT).show();
- }
- }
- }, mMultiCameraModule.getMyCameraHandler());
- } catch (CameraAccessException | IllegalStateException | IOException e) {
- e.printStackTrace();
- }
- }
-
- public void stopRecordingVideo(int id) {
- Log.v(TAG, " stopRecordingVideo " + id);
- mIsRecordingVideos[id] = false;
- try {
- mMediaRecorders[id].setOnErrorListener(null);
- mMediaRecorders[id].setOnInfoListener(null);
- // Stop recording
- mMediaRecorders[id].stop();
- mMediaRecorders[id].reset();
- saveVideo(id);
- keepScreenOnAwhile();
- // release media recorder
- releaseMediaRecorder(id);
- releaseAudioFocus();
- } catch (RuntimeException e) {
- Log.w(TAG, "MediaRecoder stop fail", e);
- if (mVideoFilenames[id] != null) deleteVideoFile(mVideoFilenames[id]);
- }
-
- mMultiCameraUI.showRecordingUI(false);
- if (null != mActivity) {
- Toast.makeText(mActivity, "Video saved: " + mNextVideoAbsolutePaths[id],
- Toast.LENGTH_SHORT).show();
- Log.d(TAG, "Video saved: " + mNextVideoAbsolutePaths[id]);
- }
- mNextVideoAbsolutePaths[id] = null;
- if(!mPaused) {
- createCameraPreviewSession(id);
- }
- }
-
- private final MediaSaveService.OnMediaSavedListener mOnVideoSavedListener =
- new MediaSaveService.OnMediaSavedListener() {
- @Override
- public void onMediaSaved(Uri uri) {
- if (uri != null) {
- mActivity.notifyNewMedia(uri);
- mCurrentVideoUri = uri;
- }
- }
- };
-
- private void keepScreenOn() {
- mMultiCameraModule.getMainHandler().removeMessages(MultiCameraModule.CLEAR_SCREEN_DELAY);
- mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- }
-
- private void keepScreenOnAwhile() {
- mMultiCameraModule.getMainHandler().removeMessages(MultiCameraModule.CLEAR_SCREEN_DELAY);
- mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- mMultiCameraModule.getMainHandler().sendEmptyMessageDelayed(
- MultiCameraModule.CLEAR_SCREEN_DELAY, SCREEN_DELAY);
- }
-
- private void releaseMediaRecorder(int id) {
- Log.v(TAG, "Releasing media recorder.");
- cleanupEmptyFile(id);
- if (mMediaRecorders[id] != null) {
- try{
- mMediaRecorders[id].reset();
- mMediaRecorders[id].release();
- }catch (RuntimeException e) {
- e.printStackTrace();
- }
- mMediaRecorders[id] = null;
- }
- }
-
- /*
- * Make sure we're not recording music playing in the background, ask the
- * MediaPlaybackService to pause playback.
- */
- private void requestAudioFocus() {
- AudioManager am = (AudioManager)mActivity.getSystemService(Context.AUDIO_SERVICE);
- // Send request to obtain audio focus. This will stop other
- // music stream.
- int result = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
- AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
- if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
- Log.v(TAG, "Audio focus request failed");
- }
- }
-
- private void releaseAudioFocus() {
- AudioManager am = (AudioManager)mActivity.getSystemService(Context.AUDIO_SERVICE);
- int result = am.abandonAudioFocus(null);
- if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
- Log.v(TAG, "Audio focus release failed");
- }
- }
-
- private void cleanupEmptyFile(int id) {
- if (mVideoFilenames[id] != null) {
- File f = new File(mVideoFilenames[id]);
- if (f.length() == 0 && f.delete()) {
- Log.v(TAG, "Empty video file deleted: " + mVideoFilenames[id]);
- mVideoFilenames[id] = null;
- }
- }
- }
-
- private void updateAudioEncoder() {
- String audioEncoderStr = mActivity.getResources().getString(
- R.string.pref_camera_audioencoder_default);
- if (mLocalSharedPref != null) {
- audioEncoderStr = mLocalSharedPref.getString(MultiSettingsActivity.KEY_AUDIO_ENCODER,
- audioEncoderStr);
- }
- mAudioEncoder = SettingTranslation.getAudioEncoder(audioEncoderStr);
- }
-
- private void updateVideoRotation() {
- String defaultValue = mActivity.getResources().getString(
- R.string.pref_camera_video_rotation_default);
- if (mLocalSharedPref != null) {
- mVideoRotation = mLocalSharedPref.getString(MultiSettingsActivity.KEY_VIDEO_ROTATION,
- defaultValue);
- }
- }
-
- private void deleteVideoFile(String fileName) {
- Log.v(TAG, "Deleting video " + fileName);
- File f = new File(fileName);
- if (!f.delete()) {
- Log.v(TAG, "Could not delete " + fileName);
- }
- }
-
- private void saveVideo(int id) {
- File origFile = new File(mVideoFilenames[id]);
- if (!origFile.exists() || origFile.length() <= 0) {
- Log.e(TAG, "Invalid file");
- mCurrentVideoValues[id] = null;
- return;
- }
-
- long duration = 0L;
- MediaMetadataRetriever retriever = new MediaMetadataRetriever();
- try {
- retriever.setDataSource(mVideoFilenames[id]);
- duration = Long.valueOf(retriever.extractMetadata(
- MediaMetadataRetriever.METADATA_KEY_DURATION));
- } catch (IllegalArgumentException e) {
- Log.e(TAG, "cannot access the file");
- }
- retriever.release();
- mActivity.getMediaSaveService().addVideo(mVideoFilenames[id],
- duration, mCurrentVideoValues[id],
- mOnVideoSavedListener, mContentResolver);
- Log.v(TAG, "saveVideo mVideoFilenames[id] :" + mVideoFilenames[id]);
- mCurrentVideoValues[id] = null;
- }
-
- private void updateRecordingTime(int id) {
- if (!mIsRecordingVideos[id] || id == 0) {
- return;
- }
-
- if (mMediaRecorderPausing) {
- return;
- }
-
- long now = SystemClock.uptimeMillis();
- long delta = now - mRecordingStartTime + mRecordingTotalTime;
-
- // Starting a minute before reaching the max duration
- // limit, we'll countdown the remaining time instead.
- boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0
- && delta >= mMaxVideoDurationInMs - 60000);
-
- long deltaAdjusted = delta;
- if (countdownRemainingTime) {
- deltaAdjusted = Math.max(0, mMaxVideoDurationInMs - deltaAdjusted) + 999;
- }
- String text;
- long targetNextUpdateDelay;
- if (!mCaptureTimeLapse) {
- text = CameraUtil.millisecondToTimeString(deltaAdjusted, false);
- targetNextUpdateDelay = 1000;
- } else {
- // The length of time lapse video is different from the length
- // of the actual wall clock time elapsed. Display the video length
- // only in format hh:mm:ss.dd, where dd are the centi seconds.
- text = CameraUtil.millisecondToTimeString(getTimeLapseVideoLength(delta), true);
- targetNextUpdateDelay = mTimeBetweenTimeLapseFrameCaptureMs;
- }
- mMultiCameraUI.setRecordingTime(text);
- if (mRecordingTimeCountsDown != countdownRemainingTime) {
- // Avoid setting the color on every update, do it only
- // when it needs changing.
- mRecordingTimeCountsDown = countdownRemainingTime;
-
- int color = mActivity.getResources().getColor(countdownRemainingTime
- ? R.color.recording_time_remaining_text
- : R.color.recording_time_elapsed_text);
-
- mMultiCameraUI.setRecordingTimeTextColor(color);
- }
- long actualNextUpdateDelay = targetNextUpdateDelay - (delta % targetNextUpdateDelay);
- mMultiCameraModule.getMainHandler().postDelayed(new Runnable() {
- @Override
- public void run() {
- updateRecordingTime(id);
- }
- }, actualNextUpdateDelay);
- }
-
- private long getTimeLapseVideoLength(long deltaMs) {
- // For better approximation calculate fractional number of frames captured.
- // This will update the video time at a higher resolution.
- double numberOfFrames = (double) deltaMs / mTimeBetweenTimeLapseFrameCaptureMs;
- return (long) (numberOfFrames / mProfile.videoFrameRate * 1000);
- }
-
- /**
- * Update the camera preview. {@link #startPreview()} needs to be called in advance.
- */
- private void updatePreview(int id) {
- if (null == mCameraDevices[id]) {
- return;
- }
- try {
- setUpCaptureRequestBuilder(mPreviewRequestBuilders[id]);
- mCameraPreviewSessions[id].setRepeatingRequest(mPreviewRequestBuilders[id].build(),
- null, mMultiCameraModule.getMyCameraHandler());
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
-
- private void setUpCaptureRequestBuilder(CaptureRequest.Builder builder) {
- builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
- }
-
- private String generateVideoFilename(int outputFileFormat, int id) {
- long dateTaken = System.currentTimeMillis();
- String title = createName(dateTaken);
- String filename = title + "_"+ id + CameraUtil.convertOutputFormatToFileExt(outputFileFormat);
- String mime = CameraUtil.convertOutputFormatToMimeType(outputFileFormat);
- String path = Storage.DIRECTORY + '/' + filename;
- mCurrentVideoValues[id] = new ContentValues(9);
- mCurrentVideoValues[id].put(MediaStore.Video.Media.TITLE, title);
- mCurrentVideoValues[id].put(MediaStore.Video.Media.DISPLAY_NAME, filename);
- mCurrentVideoValues[id].put(MediaStore.Video.Media.DATE_TAKEN, dateTaken);
- mCurrentVideoValues[id].put(MediaStore.MediaColumns.DATE_MODIFIED, dateTaken / 1000);
- mCurrentVideoValues[id].put(MediaStore.Video.Media.MIME_TYPE, mime);
- mCurrentVideoValues[id].put(MediaStore.Video.Media.DATA, path);
- mCurrentVideoValues[id].put(MediaStore.Video.Media.RESOLUTION,
- "" + mVideoSize[id].getWidth() + "x" + mVideoSize[id].getHeight());
- Location loc = mLocationManager.getCurrentLocation();
- if (loc != null) {
- mCurrentVideoValues[id].put(MediaStore.Video.Media.LATITUDE, loc.getLatitude());
- mCurrentVideoValues[id].put(MediaStore.Video.Media.LONGITUDE, loc.getLongitude());
- }
- mVideoFilenames[id] = path;
- return path;
- }
-
- private String createName(long dateTaken) {
- Date date = new Date(dateTaken);
- SimpleDateFormat dateFormat = new SimpleDateFormat(
- mActivity.getString(R.string.video_file_name_format));
- return dateFormat.format(date);
- }
-
- private void setUpMediaRecorder(int id) throws IOException {
- if (null == mActivity) {
- return;
- }
- Log.v(TAG, " setUpMediaRecorder " + id);
- int size = CameraSettings.VIDEO_QUALITY_TABLE.get(mVideoSize[id].getWidth() + "x"
- + mVideoSize[id].getHeight());
- if (CamcorderProfile.hasProfile(id, size)) {
- mProfile = CamcorderProfile.get(id, size);
- } else {
- warningToast(R.string.error_app_unsupported_profile);
- throw new IllegalArgumentException("error_app_unsupported_profile");
- }
-
- if (mMediaRecorders[id] == null) {
- mMediaRecorders[id] = new MediaRecorder();
- }
- mMediaRecorders[id].setAudioSource(MediaRecorder.AudioSource.MIC);
- mMediaRecorders[id].setVideoSource(MediaRecorder.VideoSource.SURFACE);
- mMediaRecorders[id].setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
- if (mNextVideoAbsolutePaths[id] == null || mNextVideoAbsolutePaths[id].isEmpty()) {
- mNextVideoAbsolutePaths[id] = generateVideoFilename(mProfile.fileFormat, id);
- }
-
- mMediaRecorders[id].setMaxDuration(mMaxVideoDurationInMs);
- mMediaRecorders[id].setOutputFile(mNextVideoAbsolutePaths[id]);
- mMediaRecorders[id].setVideoEncodingBitRate(10000000);
- mMediaRecorders[id].setVideoFrameRate(30);
- mMediaRecorders[id].setVideoSize(mVideoSize[id].getWidth(), mVideoSize[id].getHeight());
- mMediaRecorders[id].setVideoEncoder(MediaRecorder.VideoEncoder.H264);
- mMediaRecorders[id].setAudioEncoder(mAudioEncoder);
- int rotation = CameraUtil.getJpegRotation(id, mOrientation);
- if (mVideoRotation != null) {
- rotation += Integer.parseInt(mVideoRotation);
- rotation = rotation % 360;
- }
- mMediaRecorders[id].setOrientationHint(rotation);
- mMediaRecorders[id].prepare();
- mMediaRecorders[id].setOnErrorListener(this);
- mMediaRecorders[id].setOnInfoListener(this);
- }
-
- private void warningToast(final String msg) {
- mActivity.runOnUiThread(new Runnable() {
- public void run() {
- RotateTextToast.makeText(mActivity, msg,
- Toast.LENGTH_SHORT).show();
- }
- });
- }
-
- private void warningToast(final int sourceId) {
- warningToast(sourceId, true);
- }
-
- private void warningToast(final int sourceId, boolean isLongShow) {
- mActivity.runOnUiThread(new Runnable() {
- public void run() {
- RotateTextToast.makeText(mActivity, sourceId,
- isLongShow ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
- }
- });
- }
-
- /**
- * Shows a {@link Toast} on the UI thread.
- * @param text The message to show
- */
- private void showToast(final String text) {
- if (mActivity != null) {
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
-}
+/* + * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.camera.multi; + +import android.content.Context; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.ImageFormat; +import android.graphics.Point; +import android.hardware.camera2.CameraAccessException; +import android.hardware.camera2.CameraCaptureSession; +import android.hardware.camera2.CameraCharacteristics; +import android.hardware.camera2.CameraDevice; +import android.hardware.camera2.CaptureFailure; +import android.hardware.camera2.CameraManager; +import android.hardware.camera2.CameraMetadata; +import android.hardware.camera2.CaptureRequest; +import android.hardware.camera2.CaptureResult; +import android.hardware.camera2.TotalCaptureResult; +import android.hardware.camera2.params.OutputConfiguration; +import android.hardware.camera2.params.SessionConfiguration; +import android.hardware.camera2.params.StreamConfigurationMap; +import android.location.Location; +import android.media.AudioManager; +import android.media.CamcorderProfile; +import android.media.Image; +import android.media.ImageReader; +import android.media.MediaRecorder; +import android.media.MediaMetadataRetriever; +import android.net.Uri; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Looper; +import android.os.Message; +import android.os.SystemClock; +import android.os.ParcelFileDescriptor; +import android.provider.MediaStore; +import android.util.Log; +import android.util.Size; +import android.view.OrientationEventListener; +import android.util.SparseIntArray; +import android.view.Surface; +import android.view.SurfaceHolder; +import android.view.WindowManager; +import android.widget.Toast; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.concurrent.Executor; +import java.util.List; +import java.util.Date; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; + +import com.android.camera.CameraActivity; +import com.android.camera.CaptureModule; +import com.android.camera.CameraSettings; +import com.android.camera.ComboPreferences; +import com.android.camera.Exif; +import com.android.camera.exif.ExifInterface; +import com.android.camera.LocationManager; +import com.android.camera.MediaSaveService; +import com.android.camera.PhotoModule.NamedImages; +import com.android.camera.PhotoModule.NamedImages.NamedEntity; +import com.android.camera.SDCard; +import com.android.camera.SoundClips; +import com.android.camera.Storage; +import com.android.camera.Thumbnail; +import com.android.camera.util.CameraUtil; +import com.android.camera.util.SettingTranslation; +import com.android.camera.util.PersistUtil; +import com.android.camera.ui.RotateTextToast; + +import org.codeaurora.snapcam.R; + +public class MultiVideoModule implements MultiCamera, LocationManager.Listener, + MediaRecorder.OnErrorListener, MediaRecorder.OnInfoListener { + + private static final String TAG = "SnapCam_MultiVideoModule"; + + private static final boolean DEBUG = + (PersistUtil.getCamera2Debug() == PersistUtil.CAMERA2_DEBUG_DUMP_LOG) || + (PersistUtil.getCamera2Debug() == PersistUtil.CAMERA2_DEBUG_DUMP_ALL); + + private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90; + private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270; + private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray(); + private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray(); + + private static final int WAIT_SURFACE = 0; + private static final int OPEN_CAMERA = 1; + + private static final int SCREEN_DELAY = 2 * 60 * 1000; + + private static final int MAX_NUM_CAM = 16; + + private int mCameraListIndex = 0; + + private static final CaptureRequest.Key<Byte> override_resource_cost_validation = + new CaptureRequest.Key<>( + "org.codeaurora.qcamera3.sessionParameters.overrideResourceCostValidation", + byte.class); + + private CameraActivity mActivity; + private MultiCameraUI mMultiCameraUI; + private MultiCameraModule mMultiCameraModule; + private SharedPreferences mLocalSharedPref; + private ArrayList<CameraCharacteristics> mCharacteristics; + private CameraDevice[] mCameraDevices = new CameraDevice[MAX_NUM_CAM]; + private CameraCaptureSession[] mCameraPreviewSessions = new CameraCaptureSession[MAX_NUM_CAM]; + private ContentValues[] mCurrentVideoValues = new ContentValues[MAX_NUM_CAM]; + private ImageReader[] mImageReaders = new ImageReader[MAX_NUM_CAM]; + private MediaRecorder[] mMediaRecorders = new MediaRecorder[MAX_NUM_CAM]; + private String[] mNextVideoAbsolutePaths = new String[MAX_NUM_CAM]; + private boolean mPaused = true; + + private NamedImages mNamedImages; + + private Uri mCurrentVideoUri; + + private boolean mMediaRecorderPausing = false; + + private boolean mRecordingTimeCountsDown = false; + + private LocationManager mLocationManager; + private CamcorderProfile mProfile; + + private Size[] mVideoSize = new Size[MAX_NUM_CAM]; + private Size mPreviewSizes[] = new Size[MAX_NUM_CAM]; + private int[][] mMaxPreviewSize = new int[MAX_NUM_CAM][]; + + private boolean mCaptureTimeLapse = false; + // Default 0. If it is larger than 0, the camcorder is in time lapse mode. + private int mTimeBetweenTimeLapseFrameCaptureMs = 0; + + private String[] mVideoFilenames = new String[MAX_NUM_CAM]; + + private long mRecordingStartTime; + private long mRecordingTotalTime; + + private ParcelFileDescriptor mVideoFileDescriptor; + + // The video duration limit. 0 means no limit. + private int mMaxVideoDurationInMs; + + private int mAudioEncoder; + private String mVideoRotation; + + private SoundClips.Player mSoundPlayer; + + /** + * Whether the app is recording video now + */ + private boolean[] mIsRecordingVideos = new boolean[MAX_NUM_CAM]; + + private String[] mCameraIds; + private ArrayList<String> mCameraIDList = new ArrayList<>(); + + /** + * Orientation of the camera sensor + */ + private int mSensorOrientation; + private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; + + /** + * {@link CaptureRequest.Builder} for the camera preview + */ + private CaptureRequest.Builder[] mPreviewRequestBuilders = new CaptureRequest.Builder[MAX_NUM_CAM]; + + private Handler mCameraHandler; + private HandlerThread mCameraThread; + + private ContentResolver mContentResolver; + /** + * A {@link Semaphore} make sure the camera open callback happens first before closing the + * camera. + */ + private Semaphore mCameraOpenCloseLock = new Semaphore(3); + + public MultiVideoModule(CameraActivity activity, MultiCameraUI ui, MultiCameraModule module) { + mActivity = activity; + mMultiCameraUI = ui; + mMultiCameraModule = module; + mContentResolver = mActivity.getContentResolver(); + mLocationManager = new LocationManager(mActivity, this); + mNamedImages = new NamedImages(); + mLocalSharedPref = mActivity.getSharedPreferences( + ComboPreferences.getLocalSharedPreferencesName(mActivity, + "multi" + mMultiCameraModule.getCurrenCameraMode()), Context.MODE_PRIVATE); + startBackgroundThread(); + initCameraCharacteristics(); + } + + @Override + public void onResume(String[] ids) { + for (String id : ids) { + mCameraIDList.add(id); + } + // Set up sound playback for video record and video stop + if (mSoundPlayer == null) { + mSoundPlayer = SoundClips.getPlayer(mActivity); + } + mPaused = false; + initializeValues(); + startBackgroundThread(); + for (String id : ids) { + int cameraId = Integer.parseInt(id); + updateVideoSize(cameraId); + int index = mCameraIDList.indexOf(id); + mMultiCameraUI.setPreviewSize(index, mPreviewSizes[cameraId].getWidth(), + mPreviewSizes[cameraId].getHeight()); + } + } + + @Override + public void onPause() { + mPaused = true; + if (mSoundPlayer != null) { + mSoundPlayer.release(); + mSoundPlayer = null; + } + for (String id : mCameraIds) { + int cameraId = Integer.parseInt(id); + Log.d(TAG, " onPause id :" + cameraId + " recording is :" + + (mIsRecordingVideos[cameraId] ? "STOPED" : "START")); + if (mIsRecordingVideos[cameraId]) { + stopRecordingVideo(cameraId); + } + } + + if (mCameraIDList != null) { + mCameraIDList.clear(); + } + mCameraListIndex = 0; + closeCamera(); + stopBackgroundThread(); + } + + @Override + public boolean openCamera(String[] ids) { + mCameraIds = ids; + for (String id : ids) { + mCameraIDList.add(id); + } + Message msg = Message.obtain(); + msg.what = OPEN_CAMERA; + if (mCameraHandler != null) { + mCameraHandler.sendMessage(msg); + } + return true; + } + + @Override + public void startPreview() { + + } + + @Override + public void closeSession() { + + } + + @Override + public void closeCamera() { + Log.d(TAG, "closeCamera"); + /* no need to set this in the callback and handle asynchronously. This is the same + reason as why we release the semaphore here, not in camera close callback function + as we don't have to protect the case where camera open() gets called during camera + close(). The low level framework/HAL handles the synchronization for open() + happens after close() */ + try { + // Close camera starting with AUX first + for (int i = MAX_NUM_CAM - 1; i >= 0; i--) { + if (null != mCameraDevices[i]) { + if (!mCameraOpenCloseLock.tryAcquire(2000, TimeUnit.MILLISECONDS)) { + Log.d(TAG, "Time out waiting to lock camera closing."); + throw new RuntimeException("Time out waiting to lock camera closing"); + } + Log.d(TAG, "Closing camera: " + mCameraDevices[i].getId()); + mCameraDevices[i].close(); + mCameraDevices[i] = null; + mCameraPreviewSessions[i] = null; + } + if (null != mImageReaders[i]) { + mImageReaders[i].close(); + mImageReaders[i] = null; + } + } + } catch (InterruptedException e) { + mCameraOpenCloseLock.release(); + throw new RuntimeException("Interrupted while trying to lock camera closing.", e); + } catch (IllegalStateException e) { + e.printStackTrace(); + } finally { + mCameraOpenCloseLock.release(); + } + } + + @Override + public void onVideoButtonClick(String[] ids) { + checkAndPlayShutterSound(mIsRecordingVideos[0]); + for (String id : ids) { + int cameraId = Integer.parseInt(id); + Log.d(TAG, " onVideoButtonClick id :" + cameraId + " recording is :" + + (mIsRecordingVideos[cameraId] ? "STOPED" : "START")); + if (mIsRecordingVideos[cameraId]) { + stopRecordingVideo(cameraId); + } else { + startRecordingVideo(cameraId); + } + } + } + + @Override + public void onShutterButtonClick(String[] ids) { + checkAndPlayCaptureSound(); + mMultiCameraUI.enableShutter(false); + for (String id : ids) { + Log.d(TAG, "onShutterButtonClick id :" + id); + try { + int cameraId = Integer.parseInt(id); + if (null == mActivity || null == mCameraDevices[cameraId]) { + warningToast("Camera is not ready yet to take a video snapshot."); + return; + } + final CaptureRequest.Builder captureBuilder = + mCameraDevices[cameraId].createCaptureRequest( + CameraDevice.TEMPLATE_VIDEO_SNAPSHOT); + captureBuilder.addTarget(mImageReaders[cameraId].getSurface()); + int index = mCameraIDList.indexOf(String.valueOf(cameraId)); + captureBuilder.addTarget(mMultiCameraUI.getSurfaceViewList().get( + index).getHolder().getSurface()); + // Use the same AE and AF modes as the preview. + captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, + CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); + captureBuilder.set(CaptureRequest.JPEG_THUMBNAIL_QUALITY, (byte) 80); + captureBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO); + captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, + CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); + + // Orientation + int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation(); + captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, + CameraUtil.getJpegRotation(cameraId, rotation)); + mCameraPreviewSessions[cameraId].capture(captureBuilder.build(), + mCaptureStillCallback, mMultiCameraModule.getMyCameraHandler()); + Log.d(TAG, " cameraCaptureSession" + id + " captured "); + } catch (CameraAccessException e) { + e.printStackTrace(); + } + } + } + + @Override + public void onButtonPause(String[] ids) { + mRecordingTotalTime += SystemClock.uptimeMillis() - mRecordingStartTime; + mMediaRecorderPausing = true; + for (String id : ids) { + int cameraId = Integer.parseInt(id); + mMediaRecorders[cameraId].pause(); + } + } + + @Override + public void onButtonContinue(String[] ids) { + mMediaRecorderPausing = false; + for (String id : ids) { + int cameraId = Integer.parseInt(id); + mMediaRecorders[cameraId].resume(); + mRecordingStartTime = SystemClock.uptimeMillis(); + updateRecordingTime(cameraId); + } + } + + @Override + public void onErrorListener(int error) { + enableRecordingLocation(false); + } + + // from MediaRecorder.OnErrorListener + @Override + public void onError(MediaRecorder mr, int what, int extra) { + Log.e(TAG, "MediaRecorder error. what=" + what + ". extra=" + extra); + String[] ids = {"0", "1"}; + for (String id : ids) { + int cameraId = Integer.parseInt(id); + stopRecordingVideo(cameraId); + } + if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { + // We may have run out of space on the sdcard. + mActivity.updateStorageSpaceAndHint(); + } else { + warningToast("MediaRecorder error. what=" + what + ". extra=" + extra); + } + } + + // from MediaRecorder.OnInfoListener + @Override + public void onInfo(MediaRecorder mr, int what, int extra) { + String[] ids = {"0", "1"}; + if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) { + for (String id : ids) { + int cameraId = Integer.parseInt(id); + if (mIsRecordingVideos[cameraId]) { + stopRecordingVideo(cameraId); + } + } + } else if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) { + for (String id : ids) { + int cameraId = Integer.parseInt(id); + if (mIsRecordingVideos[cameraId]) { + stopRecordingVideo(cameraId); + } + } + // Show the toast. + RotateTextToast.makeText(mActivity, R.string.video_reach_size_limit, + Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onOrientationChanged(int orientation) { + mOrientation = orientation; + } + + @Override + public boolean isRecordingVideo() { + for (int i = 0; i < mIsRecordingVideos.length; i++) { + if (mIsRecordingVideos[i]) return true; + } + return false; + } + + private void openCameraInSequence(String id) { + CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE); + CameraCharacteristics characteristics = null; + try { + characteristics = manager.getCameraCharacteristics(id); + } catch (CameraAccessException e) { + e.printStackTrace(); + } + mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); + Log.d(TAG, "openCameraInSequence " + id + ", mSensorOrientation :" + mSensorOrientation); + try { + if (!mCameraOpenCloseLock.tryAcquire(5000, TimeUnit.MILLISECONDS)) { + Log.d(TAG, "Time out waiting to lock camera opening."); + throw new RuntimeException("Time out waiting to lock camera opening"); + } + manager.openCamera(id, mStateCallback, mMultiCameraModule.getMyCameraHandler()); + } catch (CameraAccessException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + manager = null; + characteristics = null; + } + + private void initCameraCharacteristics() { + mCharacteristics = new ArrayList<>(); + CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE); + try { + String[] cameraIdList = manager.getCameraIdList(); + Log.d(TAG, "cameraIdList size =" + cameraIdList.length); + for (int i = 0; i < cameraIdList.length; i++) { + String cameraId = cameraIdList[i]; + CameraCharacteristics characteristics + = manager.getCameraCharacteristics(cameraId); + mCharacteristics.add(i, characteristics); + try { + mMaxPreviewSize[i] = characteristics.get(CaptureModule.max_preview_size); + } catch (IllegalArgumentException e) { + Log.e(TAG, "getMaxPreviewSize no vendorTag max_preview_size:"); + } + if (mMaxPreviewSize[i] != null) { + Log.d(TAG, " init cameraId :" + cameraId + ", i :" + i + + ", maxPreviewSize :" + mMaxPreviewSize[i][0]+ "x" + + mMaxPreviewSize[i][1]); + } + } + } catch (CameraAccessException e) { + e.printStackTrace(); + } + } + + private void startBackgroundThread() { + if (mCameraThread == null) { + mCameraThread = new HandlerThread("CameraBackground"); + mCameraThread.start(); + } + if (mCameraHandler == null) { + mCameraHandler = new MyCameraHandler(mCameraThread.getLooper()); + } + } + + private void stopBackgroundThread() { + mCameraThread.quitSafely(); + try { + mCameraThread.join(); + mCameraThread = null; + mCameraHandler = null; + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + private class MyCameraHandler extends Handler { + + public MyCameraHandler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case WAIT_SURFACE: + int id = msg.arg1; + int index = mCameraIDList.indexOf(String.valueOf(id)); + Log.v(TAG, "WAIT_SURFACE id :" + id + ", index :" + index); + if (index == -1) { + break; + } + Surface surface = mMultiCameraUI.getSurfaceViewList().get(index) + .getHolder().getSurface(); + if (surface.isValid()) { + createCaptureSessions(id, surface); + } else { + mCameraHandler.sendMessageDelayed(msg, 200); + Log.v(TAG, "Surface is invalid, wait more 200ms surfaceCreated"); + } + break; + case OPEN_CAMERA: + if (mCameraListIndex == mCameraIDList.size()) { + mCameraListIndex = 0; + } else { + String cameraId = mCameraIDList.get(mCameraListIndex); + openCameraInSequence(cameraId); + mCameraListIndex ++; + Log.v(TAG, " OPEN_CAMERA cameraId :" + cameraId + ", mCameraListIndex :" + + mCameraListIndex); + } + break; + } + } + } + + private void createVideoSnapshotImageReader(int id) { + if (mImageReaders[id] != null) { + mImageReaders[id].close(); + } + mImageReaders[id] = ImageReader.newInstance(mVideoSize[id].getWidth(), + mVideoSize[id].getHeight(), + ImageFormat.JPEG, /*maxImages*/2); + mImageReaders[id].setOnImageAvailableListener( + mOnImageAvailableListener, mMultiCameraModule.getMyCameraHandler()); + } + + private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() { + + @Override + public void onOpened(CameraDevice cameraDevice) { + int id = Integer.parseInt(cameraDevice.getId()); + mCameraDevices[id] = cameraDevice; + Log.d(TAG, "onOpened " + id); + mCameraOpenCloseLock.release(); + createCameraPreviewSession(id); + mActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + mMultiCameraUI.onCameraOpened(id); + } + }); + } + + @Override + public void onDisconnected(CameraDevice cameraDevice) { + int id = Integer.parseInt(cameraDevice.getId()); + Log.d(TAG, "onDisconnected " + id); + mCameraOpenCloseLock.release(); + mCameraDevices[id] = null; + } + + @Override + public void onError(CameraDevice cameraDevice, int error) { + int id = Integer.parseInt(cameraDevice.getId()); + Log.e(TAG, "onError " + id + " " + error); + mCameraOpenCloseLock.release(); + + if (null != mActivity) { + Toast.makeText(mActivity,"open camera error id =" + id, + Toast.LENGTH_LONG).show(); + mActivity.finish(); + } + } + + @Override + public void onClosed(CameraDevice cameraDevice) { + int id = Integer.parseInt(cameraDevice.getId()); + Log.d(TAG, "onClosed " + id); + mCameraOpenCloseLock.release(); + mCameraDevices[id] = null; + } + }; + + private CameraCaptureSession.CaptureCallback mCaptureStillCallback + = new CameraCaptureSession.CaptureCallback() { + + @Override + public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, + TotalCaptureResult result) { + Log.d(TAG, " mCaptureCallback onCaptureCompleted "); + mMultiCameraModule.getMainHandler().post(new Runnable() { + @Override + public void run() { + Log.d(TAG, " enable Shutter " ); + mMultiCameraUI.enableShutter(true); + } + }); + } + + @Override + public void onCaptureFailed(CameraCaptureSession session, + CaptureRequest request, + CaptureFailure result) { + Log.d(TAG, " mCaptureCallback onCaptureFailed " ); + } + + + @Override + public void onCaptureSequenceCompleted(CameraCaptureSession session, int + sequenceId, long frameNumber) { + Log.d(TAG, " mCaptureCallback onCaptureSequenceCompleted "); + } + }; + + private MediaSaveService.OnMediaSavedListener mOnMediaSavedListener = + new MediaSaveService.OnMediaSavedListener() { + @Override + public void onMediaSaved(Uri uri) { + if (uri != null) { + mActivity.notifyNewMedia(uri); + } + } + }; + + public void enableRecordingLocation(boolean enable) { + mLocationManager.recordLocation(enable); + } + + private Size parsePictureSize(String value) { + int indexX = value.indexOf('x'); + int width = Integer.parseInt(value.substring(0, indexX)); + int height = Integer.parseInt(value.substring(indexX + 1)); + return new Size(width, height); + } + + private void updateVideoSize(int id) { + String defaultSize = mActivity.getString(R.string.pref_multi_camera_video_quality_default); + int index = mCameraIDList.indexOf(String.valueOf(id)); + String videoSize = mLocalSharedPref.getString( + MultiSettingsActivity.KEY_VIDEO_SIZES.get(index), defaultSize); + Size size = parsePictureSize(videoSize); + mVideoSize[id] = size; + Log.v(TAG, " updateVideoSize size :" + mVideoSize[id].getWidth() + "x" + + mVideoSize[id].getHeight()); + + Size[] prevSizes = getSupportedOutputSize(id, MediaRecorder.class); + mPreviewSizes[id] = getOptimalVideoPreviewSize(id, mVideoSize[id], prevSizes); + } + + private Size[] getSupportedOutputSize(int cameraId, Class cl) { + StreamConfigurationMap map = mCharacteristics.get(cameraId).get( + CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); + Size[] normal = map.getOutputSizes(cl); + Size[] high = map.getHighResolutionOutputSizes(ImageFormat.PRIVATE); + Size[] ret = new Size[normal.length + high.length]; + System.arraycopy(normal, 0, ret, 0, normal.length); + System.arraycopy(high, 0, ret, normal.length, high.length); + return ret; + } + + private Size getOptimalVideoPreviewSize(int id, Size VideoSize, Size[] prevSizes) { + Point[] points = new Point[prevSizes.length]; + + int index = 0; + int point_max[] = mMaxPreviewSize[id]; + int max_size = -1; + if (point_max != null) { + max_size = point_max[0] * point_max[1]; + } + for (Size s : prevSizes) { + if (max_size != -1) { + int size = s.getWidth() * s.getHeight(); + if (s.getWidth() == s.getHeight()) { + if (s.getWidth() > Math.max(point_max[0], point_max[1])) + continue; + } else if (size > max_size || size == 0) { + continue; + } + } + points[index++] = new Point(s.getWidth(), s.getHeight()); + } + + int optimalPickIndex = CameraUtil.getOptimalVideoPreviewSize(mActivity, points, VideoSize); + return (optimalPickIndex == -1) ? null : + new Size(points[optimalPickIndex].x, points[optimalPickIndex].y); + } + + /** + * Creates a new {@link CameraCaptureSession} for camera preview. + */ + private void createCameraPreviewSession(int id) { + // This is the output Surface we need to start preview. + int index = mCameraIDList.indexOf(String.valueOf(id)); + Log.v(TAG, "createCameraPreviewSession id :" + id + ", index :" + index); + Surface surface = mMultiCameraUI.getSurfaceViewList().get(index).getHolder().getSurface(); + + if (surface.isValid()) { + createCaptureSessions(id, surface); + } else { + Message msg = Message.obtain(); + msg.what = WAIT_SURFACE; + msg.arg1 = id; + mCameraHandler.sendMessageDelayed(msg, 200); + Log.v(TAG, "Surface is invalid, wait 200ms surfaceCreated"); + } + + } + + private void createCaptureSessions(int id, Surface surface) { + try { + // We set up a CaptureRequest.Builder with the output Surface. + mPreviewRequestBuilders[id] + = mCameraDevices[id].createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); + mPreviewRequestBuilders[id].addTarget(surface); + mPreviewRequestBuilders[id].setTag(id); + + CameraCaptureSession.StateCallback stateCallback = + new CameraCaptureSession.StateCallback() { + @Override + public void onConfigured(CameraCaptureSession cameraCaptureSession) { + // The camera is already closed + if (null == mCameraDevices[id]) { + return; + } + Log.v(TAG, " CameraCaptureSession onConfigured id :" + id); + // When the session is ready, we start displaying the preview. + mCameraPreviewSessions[id] = cameraCaptureSession; + try { + // Auto focus should be continuous for camera preview. + mPreviewRequestBuilders[id].set(CaptureRequest.CONTROL_AF_MODE, + CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); + // Finally, we start displaying the camera preview. + mCameraPreviewSessions[id].setRepeatingRequest( + mPreviewRequestBuilders[id].build(), + mCaptureCallback, mMultiCameraModule.getMyCameraHandler()); + } catch (CameraAccessException e) { + e.printStackTrace(); + } + + if (mCameraDevices[mCameraListIndex] == null) { + Message msg = Message.obtain(); + msg.what = OPEN_CAMERA; + if (mCameraHandler != null) { + mCameraHandler.sendMessage(msg); + } + } + } + + @Override + public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { + showToast("onConfigureFailed"); + } + }; + + try { + final byte enable = 1; + mPreviewRequestBuilders[id].set(override_resource_cost_validation, enable); + Log.v(TAG, " set" + override_resource_cost_validation + " is 1"); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + + List<OutputConfiguration> outConfigurations = new ArrayList<>(1); + outConfigurations.add(new OutputConfiguration(surface)); + + SessionConfiguration sessionConfiguration = new SessionConfiguration( + SessionConfiguration.SESSION_REGULAR, outConfigurations, + new HandlerExecutor(mCameraHandler), stateCallback); + sessionConfiguration.setSessionParameters(mPreviewRequestBuilders[id].build()); + mCameraDevices[id].createCaptureSession(sessionConfiguration); + + } catch (CameraAccessException e) { + e.printStackTrace(); + } + } + + private class HandlerExecutor implements Executor { + private final Handler ihandler; + + public HandlerExecutor(Handler handler) { + ihandler = handler; + } + + @Override + public void execute(Runnable runCmd) { + ihandler.post(runCmd); + } + } + + /** + * A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture. + */ + private CameraCaptureSession.CaptureCallback mCaptureCallback + = new CameraCaptureSession.CaptureCallback() { + + private void process(CaptureResult result) { + Integer afState = result.get(CaptureResult.CONTROL_AF_STATE); + Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE); + if (DEBUG) { + Log.v(TAG, "process afState :" + afState + ", aeState :" + aeState); + } + } + + @Override + public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, + CaptureResult partialResult) { + process(partialResult); + } + + @Override + public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, + TotalCaptureResult result) { + process(result); + } + + }; + + private final ImageReader.OnImageAvailableListener mOnImageAvailableListener + = new ImageReader.OnImageAvailableListener() { + + @Override + public void onImageAvailable(ImageReader reader) { + Log.v(TAG, " onImageAvailable ..."); + Image image = reader.acquireNextImage(); + long imageTime = System.currentTimeMillis(); + mNamedImages.nameNewImage(imageTime); + NamedEntity name = mNamedImages.getNextNameEntity(); + String title = (name == null) ? null : name.title; + long date = (name == null) ? -1 : name.date; + + ByteBuffer buffer = image.getPlanes()[0].getBuffer(); + byte[] bytes = new byte[buffer.remaining()]; + buffer.get(bytes); + + int orientation = 0; + ExifInterface exif = Exif.getExif(bytes); + orientation = Exif.getOrientation(exif); + String saveFormat = "jpeg"; + mActivity.getMediaSaveService().addImage(bytes, title, date, + null, image.getWidth(), image.getHeight(), orientation, exif, + mOnMediaSavedListener, mContentResolver, saveFormat); + mActivity.updateThumbnail(bytes); + image.close(); + mMultiCameraModule.updateTakingPicture(); + } + }; + + private void initializeValues() { + updateAudioEncoder(); + updateVideoRotation(); + } + + private void checkAndPlayShutterSound(boolean isStarted) { + if (mSoundPlayer != null) { + mSoundPlayer.play(isStarted? SoundClips.STOP_VIDEO_RECORDING + : SoundClips.START_VIDEO_RECORDING); + } + } + + private void checkAndPlayCaptureSound() { + if (mSoundPlayer != null) { + mSoundPlayer.play(SoundClips.SHUTTER_CLICK); + } + } + + private void closePreviewSession(int id) { + if (mCameraPreviewSessions[id] != null) { + Log.v(TAG, "closePreviewSession id :" + id); + mCameraPreviewSessions[id].close(); + mCameraPreviewSessions[id] = null; + } + } + + private void startRecordingVideo(final int id) { + int index = mCameraIDList.indexOf(String.valueOf(id)); + if (null == mCameraDevices[id] || + !mMultiCameraUI.getSurfaceViewList().get(index).isEnabled()) { + return; + } + Log.v(TAG, " startRecordingVideo " + id); + try { + closePreviewSession(id); + setUpMediaRecorder(id); + createVideoSnapshotImageReader(id); + mPreviewRequestBuilders[id] = mCameraDevices[id].createCaptureRequest( + CameraDevice.TEMPLATE_RECORD); + if (true) { + mPreviewRequestBuilders[id].set(CaptureRequest.NOISE_REDUCTION_MODE, + CaptureRequest.NOISE_REDUCTION_MODE_FAST); + } else { + mPreviewRequestBuilders[id].set(CaptureRequest.NOISE_REDUCTION_MODE, + CaptureRequest.NOISE_REDUCTION_MODE_HIGH_QUALITY); + } + List<Surface> surfaces = new ArrayList<>(); + + // Set up Surface for the camera preview + Surface previewSurface = mMultiCameraUI.getSurfaceViewList().get(index).getHolder() + .getSurface(); + surfaces.add(previewSurface); + mPreviewRequestBuilders[id].addTarget(previewSurface); + + // Set up Surface for the MediaRecorder + Surface recorderSurface = mMediaRecorders[id].getSurface(); + surfaces.add(recorderSurface); + mPreviewRequestBuilders[id].addTarget(recorderSurface); + surfaces.add(mImageReaders[id].getSurface()); + + // Start a capture session + // Once the session starts, we can update the UI and start recording + mCameraDevices[id].createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() { + + @Override + public void onConfigured(CameraCaptureSession cameraCaptureSession) { + mCameraPreviewSessions[id] = cameraCaptureSession; + updatePreview(id); + mActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + mIsRecordingVideos[id] = true; + // Start recording + mMediaRecorders[id].start(); + requestAudioFocus(); + mRecordingTotalTime = 0L; + mRecordingStartTime = SystemClock.uptimeMillis(); + mMediaRecorderPausing = false; + mMultiCameraUI.resetPauseButton(); + mMultiCameraUI.showRecordingUI(true); + updateRecordingTime(id); + keepScreenOn(); + Log.v(TAG, " startRecordingVideo done " + id); + } + }); + } + + @Override + public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { + if (null != mActivity) { + Toast.makeText(mActivity, "Configure Failed", Toast.LENGTH_SHORT).show(); + } + } + }, mMultiCameraModule.getMyCameraHandler()); + } catch (CameraAccessException | IllegalStateException | IOException e) { + e.printStackTrace(); + } + } + + public void stopRecordingVideo(int id) { + Log.v(TAG, " stopRecordingVideo " + id); + mIsRecordingVideos[id] = false; + try { + mMediaRecorders[id].setOnErrorListener(null); + mMediaRecorders[id].setOnInfoListener(null); + // Stop recording + mMediaRecorders[id].stop(); + mMediaRecorders[id].reset(); + saveVideo(id); + keepScreenOnAwhile(); + // release media recorder + releaseMediaRecorder(id); + releaseAudioFocus(); + } catch (RuntimeException e) { + Log.w(TAG, "MediaRecoder stop fail", e); + if (mVideoFilenames[id] != null) deleteVideoFile(mVideoFilenames[id]); + } + + mMultiCameraUI.showRecordingUI(false); + if (null != mActivity) { + Toast.makeText(mActivity, "Video saved: " + mNextVideoAbsolutePaths[id], + Toast.LENGTH_SHORT).show(); + Log.d(TAG, "Video saved: " + mNextVideoAbsolutePaths[id]); + } + mNextVideoAbsolutePaths[id] = null; + if(!mPaused) { + createCameraPreviewSession(id); + } + } + + private final MediaSaveService.OnMediaSavedListener mOnVideoSavedListener = + new MediaSaveService.OnMediaSavedListener() { + @Override + public void onMediaSaved(Uri uri) { + if (uri != null) { + mActivity.notifyNewMedia(uri); + mCurrentVideoUri = uri; + } + } + }; + + private void keepScreenOn() { + mMultiCameraModule.getMainHandler().removeMessages(MultiCameraModule.CLEAR_SCREEN_DELAY); + mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + + private void keepScreenOnAwhile() { + mMultiCameraModule.getMainHandler().removeMessages(MultiCameraModule.CLEAR_SCREEN_DELAY); + mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + mMultiCameraModule.getMainHandler().sendEmptyMessageDelayed( + MultiCameraModule.CLEAR_SCREEN_DELAY, SCREEN_DELAY); + } + + private void releaseMediaRecorder(int id) { + Log.v(TAG, "Releasing media recorder."); + cleanupEmptyFile(id); + if (mMediaRecorders[id] != null) { + try{ + mMediaRecorders[id].reset(); + mMediaRecorders[id].release(); + }catch (RuntimeException e) { + e.printStackTrace(); + } + mMediaRecorders[id] = null; + } + } + + /* + * Make sure we're not recording music playing in the background, ask the + * MediaPlaybackService to pause playback. + */ + private void requestAudioFocus() { + AudioManager am = (AudioManager)mActivity.getSystemService(Context.AUDIO_SERVICE); + // Send request to obtain audio focus. This will stop other + // music stream. + int result = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); + if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) { + Log.v(TAG, "Audio focus request failed"); + } + } + + private void releaseAudioFocus() { + AudioManager am = (AudioManager)mActivity.getSystemService(Context.AUDIO_SERVICE); + int result = am.abandonAudioFocus(null); + if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) { + Log.v(TAG, "Audio focus release failed"); + } + } + + private void cleanupEmptyFile(int id) { + if (mVideoFilenames[id] != null) { + File f = new File(mVideoFilenames[id]); + if (f.length() == 0 && f.delete()) { + Log.v(TAG, "Empty video file deleted: " + mVideoFilenames[id]); + mVideoFilenames[id] = null; + } + } + } + + private void updateAudioEncoder() { + String audioEncoderStr = mActivity.getResources().getString( + R.string.pref_camera_audioencoder_default); + if (mLocalSharedPref != null) { + audioEncoderStr = mLocalSharedPref.getString(MultiSettingsActivity.KEY_AUDIO_ENCODER, + audioEncoderStr); + } + mAudioEncoder = SettingTranslation.getAudioEncoder(audioEncoderStr); + } + + private void updateVideoRotation() { + String defaultValue = mActivity.getResources().getString( + R.string.pref_camera_video_rotation_default); + if (mLocalSharedPref != null) { + mVideoRotation = mLocalSharedPref.getString(MultiSettingsActivity.KEY_VIDEO_ROTATION, + defaultValue); + } + } + + private void deleteVideoFile(String fileName) { + Log.v(TAG, "Deleting video " + fileName); + File f = new File(fileName); + if (!f.delete()) { + Log.v(TAG, "Could not delete " + fileName); + } + } + + private void saveVideo(int id) { + File origFile = new File(mVideoFilenames[id]); + if (!origFile.exists() || origFile.length() <= 0) { + Log.e(TAG, "Invalid file"); + mCurrentVideoValues[id] = null; + return; + } + + long duration = 0L; + MediaMetadataRetriever retriever = new MediaMetadataRetriever(); + try { + retriever.setDataSource(mVideoFilenames[id]); + duration = Long.valueOf(retriever.extractMetadata( + MediaMetadataRetriever.METADATA_KEY_DURATION)); + } catch (IllegalArgumentException e) { + Log.e(TAG, "cannot access the file"); + } + retriever.release(); + mActivity.getMediaSaveService().addVideo(mVideoFilenames[id], + duration, mCurrentVideoValues[id], + mOnVideoSavedListener, mContentResolver); + Log.v(TAG, "saveVideo mVideoFilenames[id] :" + mVideoFilenames[id]); + mCurrentVideoValues[id] = null; + } + + private void updateRecordingTime(int id) { + if (!mIsRecordingVideos[id] || id == 0) { + return; + } + + if (mMediaRecorderPausing) { + return; + } + + long now = SystemClock.uptimeMillis(); + long delta = now - mRecordingStartTime + mRecordingTotalTime; + + // Starting a minute before reaching the max duration + // limit, we'll countdown the remaining time instead. + boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 + && delta >= mMaxVideoDurationInMs - 60000); + + long deltaAdjusted = delta; + if (countdownRemainingTime) { + deltaAdjusted = Math.max(0, mMaxVideoDurationInMs - deltaAdjusted) + 999; + } + String text; + long targetNextUpdateDelay; + if (!mCaptureTimeLapse) { + text = CameraUtil.millisecondToTimeString(deltaAdjusted, false); + targetNextUpdateDelay = 1000; + } else { + // The length of time lapse video is different from the length + // of the actual wall clock time elapsed. Display the video length + // only in format hh:mm:ss.dd, where dd are the centi seconds. + text = CameraUtil.millisecondToTimeString(getTimeLapseVideoLength(delta), true); + targetNextUpdateDelay = mTimeBetweenTimeLapseFrameCaptureMs; + } + mMultiCameraUI.setRecordingTime(text); + if (mRecordingTimeCountsDown != countdownRemainingTime) { + // Avoid setting the color on every update, do it only + // when it needs changing. + mRecordingTimeCountsDown = countdownRemainingTime; + + int color = mActivity.getResources().getColor(countdownRemainingTime + ? R.color.recording_time_remaining_text + : R.color.recording_time_elapsed_text); + + mMultiCameraUI.setRecordingTimeTextColor(color); + } + long actualNextUpdateDelay = targetNextUpdateDelay - (delta % targetNextUpdateDelay); + mMultiCameraModule.getMainHandler().postDelayed(new Runnable() { + @Override + public void run() { + updateRecordingTime(id); + } + }, actualNextUpdateDelay); + } + + private long getTimeLapseVideoLength(long deltaMs) { + // For better approximation calculate fractional number of frames captured. + // This will update the video time at a higher resolution. + double numberOfFrames = (double) deltaMs / mTimeBetweenTimeLapseFrameCaptureMs; + return (long) (numberOfFrames / mProfile.videoFrameRate * 1000); + } + + /** + * Update the camera preview. {@link #startPreview()} needs to be called in advance. + */ + private void updatePreview(int id) { + if (null == mCameraDevices[id]) { + return; + } + try { + setUpCaptureRequestBuilder(mPreviewRequestBuilders[id]); + mCameraPreviewSessions[id].setRepeatingRequest(mPreviewRequestBuilders[id].build(), + null, mMultiCameraModule.getMyCameraHandler()); + } catch (CameraAccessException e) { + e.printStackTrace(); + } + } + + private void setUpCaptureRequestBuilder(CaptureRequest.Builder builder) { + builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); + } + + private String generateVideoFilename(int outputFileFormat, int id) { + long dateTaken = System.currentTimeMillis(); + String title = createName(dateTaken); + String filename = title + "_"+ id + CameraUtil.convertOutputFormatToFileExt(outputFileFormat); + String mime = CameraUtil.convertOutputFormatToMimeType(outputFileFormat); + String path; + if (Storage.isSaveSDCard() && SDCard.instance().isWriteable()) { + path = SDCard.instance().getDirectory() + '/' + filename; + } else { + path = Storage.DIRECTORY + '/' + filename; + } + mCurrentVideoValues[id] = new ContentValues(9); + mCurrentVideoValues[id].put(MediaStore.Video.Media.TITLE, title); + mCurrentVideoValues[id].put(MediaStore.Video.Media.DISPLAY_NAME, filename); + mCurrentVideoValues[id].put(MediaStore.Video.Media.DATE_TAKEN, dateTaken); + mCurrentVideoValues[id].put(MediaStore.MediaColumns.DATE_MODIFIED, dateTaken / 1000); + mCurrentVideoValues[id].put(MediaStore.Video.Media.MIME_TYPE, mime); + mCurrentVideoValues[id].put(MediaStore.Video.Media.DATA, path); + mCurrentVideoValues[id].put(MediaStore.Video.Media.RESOLUTION, + "" + mVideoSize[id].getWidth() + "x" + mVideoSize[id].getHeight()); + Location loc = mLocationManager.getCurrentLocation(); + if (loc != null) { + mCurrentVideoValues[id].put(MediaStore.Video.Media.LATITUDE, loc.getLatitude()); + mCurrentVideoValues[id].put(MediaStore.Video.Media.LONGITUDE, loc.getLongitude()); + } + mVideoFilenames[id] = path; + return path; + } + + private String createName(long dateTaken) { + Date date = new Date(dateTaken); + SimpleDateFormat dateFormat = new SimpleDateFormat( + mActivity.getString(R.string.video_file_name_format)); + return dateFormat.format(date); + } + + private void setUpMediaRecorder(int id) throws IOException { + if (null == mActivity) { + return; + } + Log.v(TAG, " setUpMediaRecorder " + id); + int size = CameraSettings.VIDEO_QUALITY_TABLE.get(mVideoSize[id].getWidth() + "x" + + mVideoSize[id].getHeight()); + if (CamcorderProfile.hasProfile(id, size)) { + mProfile = CamcorderProfile.get(id, size); + } else { + warningToast(R.string.error_app_unsupported_profile); + throw new IllegalArgumentException("error_app_unsupported_profile"); + } + + if (mMediaRecorders[id] == null) { + mMediaRecorders[id] = new MediaRecorder(); + } + mMediaRecorders[id].setAudioSource(MediaRecorder.AudioSource.MIC); + mMediaRecorders[id].setVideoSource(MediaRecorder.VideoSource.SURFACE); + mMediaRecorders[id].setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); + if (mNextVideoAbsolutePaths[id] == null || mNextVideoAbsolutePaths[id].isEmpty()) { + mNextVideoAbsolutePaths[id] = generateVideoFilename(mProfile.fileFormat, id); + } + + mMediaRecorders[id].setMaxDuration(mMaxVideoDurationInMs); + mMediaRecorders[id].setOutputFile(mNextVideoAbsolutePaths[id]); + mMediaRecorders[id].setVideoEncodingBitRate(10000000); + mMediaRecorders[id].setVideoFrameRate(30); + mMediaRecorders[id].setVideoSize(mVideoSize[id].getWidth(), mVideoSize[id].getHeight()); + mMediaRecorders[id].setVideoEncoder(MediaRecorder.VideoEncoder.H264); + mMediaRecorders[id].setAudioEncoder(mAudioEncoder); + int rotation = CameraUtil.getJpegRotation(id, mOrientation); + if (mVideoRotation != null) { + rotation += Integer.parseInt(mVideoRotation); + rotation = rotation % 360; + } + mMediaRecorders[id].setOrientationHint(rotation); + mMediaRecorders[id].prepare(); + mMediaRecorders[id].setOnErrorListener(this); + mMediaRecorders[id].setOnInfoListener(this); + } + + private void warningToast(final String msg) { + mActivity.runOnUiThread(new Runnable() { + public void run() { + RotateTextToast.makeText(mActivity, msg, + Toast.LENGTH_SHORT).show(); + } + }); + } + + private void warningToast(final int sourceId) { + warningToast(sourceId, true); + } + + private void warningToast(final int sourceId, boolean isLongShow) { + mActivity.runOnUiThread(new Runnable() { + public void run() { + RotateTextToast.makeText(mActivity, sourceId, + isLongShow ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show(); + } + }); + } + + /** + * Shows a {@link Toast} on the UI thread. + * @param text The message to show + */ + private void showToast(final String text) { + if (mActivity != null) { + mActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show(); + } + }); + } + } +} |
