1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.camera.one.v2.initialization; 18 19 import android.view.Surface; 20 21 import com.android.camera.one.OneCamera; 22 import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionProxy; 23 import com.android.camera.util.ApiHelper; 24 import com.google.common.util.concurrent.AsyncFunction; 25 import com.google.common.util.concurrent.Futures; 26 import com.google.common.util.concurrent.ListenableFuture; 27 import com.google.common.util.concurrent.MoreExecutors; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** 33 * When the preview surface is available, creates a capture session, and then 34 * notifies the listener when the session is available. 35 */ 36 class PreviewStarter { 37 public interface CameraCaptureSessionCreatedListener { onCameraCaptureSessionCreated(CameraCaptureSessionProxy session, Surface previewSurface)38 public void onCameraCaptureSessionCreated(CameraCaptureSessionProxy session, Surface 39 previewSurface); 40 } 41 42 private final List<Surface> mOutputSurfaces; 43 private final CaptureSessionCreator mCaptureSessionCreator; 44 private final CameraCaptureSessionCreatedListener mSessionListener; 45 46 /** 47 * @param outputSurfaces The set of output surfaces (except for the preview 48 * surface) to use. 49 * @param captureSessionCreator 50 * @param sessionListener A callback to be invoked when the capture session 51 * has been created. It is executed on threadPoolExecutor. 52 */ PreviewStarter(List<Surface> outputSurfaces, CaptureSessionCreator captureSessionCreator, CameraCaptureSessionCreatedListener sessionListener)53 public PreviewStarter(List<Surface> outputSurfaces, 54 CaptureSessionCreator captureSessionCreator, 55 CameraCaptureSessionCreatedListener sessionListener) { 56 mOutputSurfaces = outputSurfaces; 57 mCaptureSessionCreator = captureSessionCreator; 58 mSessionListener = sessionListener; 59 } 60 61 /** 62 * See {@link OneCamera#startPreview}. 63 * 64 * @param surface The preview surface to use. 65 */ startPreview(final Surface surface)66 public ListenableFuture<Void> startPreview(final Surface surface) { 67 // When we have the preview surface, start the capture session. 68 List<Surface> surfaceList = new ArrayList<>(); 69 70 // Workaround of the face detection failure on Nexus 5 and L. (b/21039466) 71 // Need to create a capture session with the single preview stream first 72 // to lock it as the first stream. Then resend the another session with preview 73 // and JPEG stream. 74 if (ApiHelper.isLorLMr1() && ApiHelper.IS_NEXUS_5) { 75 surfaceList.add(surface); 76 mCaptureSessionCreator.createCaptureSession(surfaceList); 77 surfaceList.addAll(mOutputSurfaces); 78 } else { 79 surfaceList.addAll(mOutputSurfaces); 80 surfaceList.add(surface); 81 } 82 83 final ListenableFuture<CameraCaptureSessionProxy> sessionFuture = 84 mCaptureSessionCreator.createCaptureSession(surfaceList); 85 86 return Futures.transformAsync(sessionFuture, 87 new AsyncFunction<CameraCaptureSessionProxy, Void>() { 88 @Override 89 public ListenableFuture<Void> apply( 90 CameraCaptureSessionProxy captureSession) throws Exception { 91 mSessionListener.onCameraCaptureSessionCreated(captureSession, surface); 92 return Futures.immediateFuture(null); 93 } 94 }, MoreExecutors.directExecutor()); 95 } 96 } 97