package net.trippedout.android.shadercamera.fragments; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.res.Configuration; import android.graphics.Matrix; import android.graphics.RectF; import android.graphics.SurfaceTexture; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.hardware.camera2.CameraMetadata; import android.hardware.camera2.CaptureRequest; import android.hardware.camera2.TotalCaptureResult; import android.hardware.camera2.params.StreamConfigurationMap; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.util.Log; import android.util.Size; import android.util.SparseIntArray; import android.view.Surface; import android.view.TextureView; import android.widget.Toast; import net.trippedout.android.shadercamera.gl.CameraRenderer; import net.trippedout.android.shadercamera.view.AutoFitTextureView; import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; /** * Fragment for operating the camera, it doesnt have any UI elements, just controllers */ public class CameraFragment extends Fragment { private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); private static final String TAG = "CameraFragment"; static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } /** * An {@link AutoFitTextureView} for camera preview. */ private AutoFitTextureView mTextureView; /** * A refernce to the opened {@link android.hardware.camera2.CameraDevice}. */ private CameraDevice mCameraDevice; /** * A reference to the current {@link android.hardware.camera2.CameraCaptureSession} for preview. */ private CameraCaptureSession mPreviewSession; private CameraRenderer mRenderer; /** * {@link android.view.TextureView.SurfaceTextureListener} handles several lifecycle events on a * {@link android.view.TextureView}. */ public static class CameraTextureListener implements TextureView.SurfaceTextureListener { private final Context mContext; private final Object mRendererClass; private CameraFragment mFragment; private CameraRenderer mRenderer; public <T extends CameraRenderer> CameraTextureListener(OnRendererCreatedListener listener, CameraFragment frag, Class<T> renderer) { mContext = (Context)listener; //cheating since we know itll be an activity mFragment = frag; mRendererClass = renderer; mFragment.setSurfaceTextureListener(this); mOnRendererCreatedListener = listener; } @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { Log.d(TAG, "onSurfaceTextureAvail() " + width + ", " + height); try { //like, why... mRenderer = (CameraRenderer)((Class)mRendererClass).getConstructors()[0].newInstance(mContext, surface, width, height); if(mOnRendererCreatedListener != null) mOnRendererCreatedListener.onRendererCreated(mRenderer); } catch (java.lang.InstantiationException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } mFragment.setRenderer(mRenderer); mFragment.configureTransform(width, height); mFragment.openCamera(width, height); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) { Log.d(TAG, "onSurfaceTextureSizeChanged() " + width + ", " + height); mFragment.configureTransform(width, height); } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { Log.d(TAG, "onDestroy()"); return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { } /** * interface for pushing back the renderer once its created */ public interface OnRendererCreatedListener { public void onRendererCreated(CameraRenderer renderer); } private OnRendererCreatedListener mOnRendererCreatedListener; } private TextureView.SurfaceTextureListener mSurfaceTextureListener; /** * The {@link android.util.Size} of camera preview. */ private Size mPreviewSize; /** * The {@link android.util.Size} of video recording. */ private Size mVideoSize; /** * Camera preview. */ private CaptureRequest.Builder mPreviewBuilder; /** * MediaRecorder */ private MediaRecorder mMediaRecorder; /** * Whether the app is recording video now */ private boolean mIsRecordingVideo; /** * An additional thread for running tasks that shouldn't block the UI. */ private HandlerThread mBackgroundThread; /** * A {@link android.os.Handler} for running tasks in the background. */ private Handler mBackgroundHandler; /** * Use these for changing which camera to use on start */ public static final int CAMERA_PRIMARY = 0; /** * The id of what is typically the forward facing camera. * If this fails, use {@link #CAMERA_PRIMARY}, as it might be the only camera registered. */ public static final int CAMERA_FORWARD = 1; protected int mCameraToUse = CAMERA_PRIMARY; /** * Set which camera to use, defaults to {@link #CAMERA_PRIMARY}. * * @param camera_id can also be {@link #CAMERA_FORWARD} for forward facing, but use primary if that fails. */ public void setCameraToUse(int camera_id) { mCameraToUse = camera_id; } /** * A {@link java.util.concurrent.Semaphore} to prevent the app from exiting before closing the camera. */ private Semaphore mCameraOpenCloseLock = new Semaphore(1); /** * {@link android.hardware.camera2.CameraDevice.StateCallback} is called when {@link android.hardware.camera2.CameraDevice} changes its status. */ private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(CameraDevice cameraDevice) { mCameraDevice = cameraDevice; startPreview(); mCameraOpenCloseLock.release(); if (null != mTextureView) { configureTransform(mTextureView.getWidth(), mTextureView.getHeight()); } } @Override public void onDisconnected(CameraDevice cameraDevice) { mCameraOpenCloseLock.release(); cameraDevice.close(); mCameraDevice = null; } @Override public void onError(CameraDevice cameraDevice, int error) { mCameraOpenCloseLock.release(); cameraDevice.close(); mCameraDevice = null; Activity activity = getActivity(); if (null != activity) { activity.finish(); } } }; public static CameraFragment newInstance() { CameraFragment fragment = new CameraFragment(); fragment.setRetainInstance(true); return fragment; } /** * In this sample, we choose a video size with 3x4 aspect ratio. Also, we don't use sizes larger * than 1080p, since MediaRecorder cannot handle such a high-resolution video. * * @param choices The list of available sizes * @return The video size */ private static Size chooseVideoSize(Size[] choices) { for (Size size : choices) { if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) { return size; } } Log.e(TAG, "Couldn't find any suitable video size"); return choices[choices.length - 1]; } /** * Given {@code choices} of {@code Size}s supported by a camera, chooses the smallest one whose * width and height are at least as large as the respective requested values, and whose aspect * ratio matches with the specified value. * * @param choices The list of sizes that the camera supports for the intended output class * @param width The minimum desired width * @param height The minimum desired height * @param aspectRatio The aspect ratio * @return The optimal {@code Size}, or an arbitrary one if none were big enough */ private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) { // Collect the supported resolutions that are at least as big as the preview Surface List<Size> bigEnough = new ArrayList<Size>(); int w = aspectRatio.getWidth(); int h = aspectRatio.getHeight(); for (Size option : choices) { if (option.getHeight() == option.getWidth() * h / w && option.getWidth() >= width && option.getHeight() >= height) { bigEnough.add(option); } } // Pick the smallest of those, assuming we found any if (bigEnough.size() > 0) { return Collections.min(bigEnough, new CompareSizesByArea()); } else { Log.e(TAG, "Couldn't find any suitable preview size"); return choices[0]; } } @Override public void onResume() { startBackgroundThread(); if (mTextureView.isAvailable()) { openCamera(mTextureView.getWidth(), mTextureView.getHeight()); } else { mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); } super.onResume(); } @Override public void onPause() { closeCamera(); stopBackgroundThread(); if(mRenderer != null) mRenderer.onPause(); super.onPause(); } public void toggleRecording() { if (mIsRecordingVideo) { stopRecordingVideo(); } else { startRecordingVideo(); } } /** * Starts a background thread and its {@link android.os.Handler}. */ private void startBackgroundThread() { mBackgroundThread = new HandlerThread("CameraBackground"); mBackgroundThread.start(); mBackgroundHandler = new Handler(mBackgroundThread.getLooper()); } /** * Stops the background thread and its {@link android.os.Handler}. */ private void stopBackgroundThread() { mBackgroundThread.quitSafely(); try { mBackgroundThread.join(); mBackgroundThread = null; mBackgroundHandler = null; } catch (InterruptedException e) { e.printStackTrace(); } } /** * Tries to open a {@link android.hardware.camera2.CameraDevice}. The result is listened by `mStateCallback`. */ public void openCamera(int width, int height) { final Activity activity = getActivity(); if (null == activity || activity.isFinishing()) { return; } CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); try { if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) { throw new RuntimeException("Time out waiting to lock camera opening."); } String cameraId = manager.getCameraIdList()[mCameraToUse]; // Choose the sizes for camera preview and video recording CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class)); mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, mVideoSize); int orientation = getResources().getConfiguration().orientation; Log.d(TAG, "orientation: " + orientation); if (orientation == Configuration.ORIENTATION_LANDSCAPE) { mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight()); mRenderer.setAspectRatio((float)mPreviewSize.getWidth() / mPreviewSize.getHeight()); configureTransform(width, height); } else { mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth()); mRenderer.setAspectRatio((float)mPreviewSize.getHeight() / mPreviewSize.getWidth()); configureTransform(height, width); } mMediaRecorder = new MediaRecorder(); manager.openCamera(cameraId, mStateCallback, null); } catch (CameraAccessException e) { Toast.makeText(activity, "Cannot access the camera.", Toast.LENGTH_SHORT).show(); activity.finish(); } catch (NullPointerException e) { // Currently an NPE is thrown when the Camera2API is used but not supported on the // device this code runs. new ErrorDialog().show(getFragmentManager(), "dialog"); } catch (InterruptedException e) { throw new RuntimeException("Interrupted while trying to lock camera opening."); } } private void closeCamera() { try { mCameraOpenCloseLock.acquire(); if (null != mCameraDevice) { mCameraDevice.close(); mCameraDevice = null; } if (null != mMediaRecorder) { mMediaRecorder.release(); mMediaRecorder = null; } } catch (InterruptedException e) { throw new RuntimeException("Interrupted while trying to lock camera closing."); } finally { mCameraOpenCloseLock.release(); } } /** * Start the camera preview. */ private void startPreview() { if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) { return; } try { Log.d(TAG, "preview size: " + mPreviewSize.getWidth() + ", " + mPreviewSize.getHeight()); setUpMediaRecorder(); SurfaceTexture texture = mRenderer.getVideoTexture(); assert texture != null; texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); List<Surface> surfaces = new ArrayList<>(); Surface previewSurface = new Surface(texture); surfaces.add(previewSurface); mPreviewBuilder.addTarget(previewSurface); Surface recorderSurface = mMediaRecorder.getSurface(); surfaces.add(recorderSurface); mPreviewBuilder.addTarget(recorderSurface); mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession cameraCaptureSession) { mPreviewSession = cameraCaptureSession; updatePreview(); } @Override public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { Activity activity = getActivity(); if (null != activity) { Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show(); } } }, mBackgroundHandler); } catch (CameraAccessException | IOException e) { e.printStackTrace(); } } /** * Update the camera preview. {@link #startPreview()} needs to be called in advance. */ private void updatePreview() { if (null == mCameraDevice) { return; } try { Log.d(TAG, "updatePreview()"); setUpCaptureRequestBuilder(mPreviewBuilder); HandlerThread thread = new HandlerThread("CameraPreview"); thread.start(); mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), captureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } private CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); } }; private void setUpCaptureRequestBuilder(CaptureRequest.Builder builder) { builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); } /** * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. * This method should not to be called until the camera preview size is determined in * openCamera, or until the size of `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` */ public void configureTransform(int viewWidth, int viewHeight) { Activity activity = getActivity(); if (null == mTextureView || null == mPreviewSize || null == activity) { return; } Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Log.d(TAG, "rotation: " + rotation); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = Math.max( (float) viewHeight / mPreviewSize.getHeight(), (float) viewWidth / mPreviewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } mTextureView.setTransform(matrix); } private void setUpMediaRecorder() throws IOException { final Activity activity = getActivity(); if (null == activity) { return; } // mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setOutputFile(getVideoFile(activity).getAbsolutePath()); mMediaRecorder.setVideoEncodingBitRate(10000000); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight()); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); // mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int orientation = ORIENTATIONS.get(rotation); mMediaRecorder.setOrientationHint(orientation); mMediaRecorder.prepare(); } private File getVideoFile(Context context) { return new File(context.getExternalFilesDir(null), "video.mp4"); } private void startRecordingVideo() { try { // UI mIsRecordingVideo = true; // Start recording mMediaRecorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } } private void stopRecordingVideo() { // UI mIsRecordingVideo = false; // Stop recording mMediaRecorder.stop(); mMediaRecorder.reset(); Activity activity = getActivity(); if (null != activity) { Toast.makeText(activity, "Video saved: " + getVideoFile(activity), Toast.LENGTH_LONG).show(); } startPreview(); } /** * Set the surfaceTextureListener for use with camera * @param surfaceTextureListener */ public void setSurfaceTextureListener(TextureView.SurfaceTextureListener surfaceTextureListener) { this.mSurfaceTextureListener = surfaceTextureListener; } public void setTextureView(AutoFitTextureView textureView) { mTextureView = textureView; } public void setRenderer(CameraRenderer renderer) { this.mRenderer = renderer; } /** * Compares two {@code Size}s based on their areas. */ static class CompareSizesByArea implements Comparator<Size> { @Override public int compare(Size lhs, Size rhs) { // We cast here to ensure the multiplications won't overflow return Long.signum((long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight()); } } public static class ErrorDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity activity = getActivity(); return new AlertDialog.Builder(activity) .setMessage("This device doesn't support Camera2 API.") .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { activity.finish(); } }) .create(); } } }