1 /*
2  * Copyright (C) 2015 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.burst;
18 
19 import android.view.Surface;
20 
21 import com.android.camera.async.Lifetime;
22 import com.android.camera.async.MainThread;
23 import com.android.camera.one.v2.commands.CameraCommandExecutor;
24 import com.android.camera.one.v2.core.FrameServer;
25 import com.android.camera.one.v2.core.Request;
26 import com.android.camera.one.v2.core.RequestBuilder;
27 import com.android.camera.one.v2.sharedimagereader.ManagedImageReader;
28 import com.google.common.base.Preconditions;
29 
30 import javax.annotation.Nullable;
31 import javax.annotation.ParametersAreNonnullByDefault;
32 
33 @ParametersAreNonnullByDefault
34 public class BurstTakerImpl implements BurstTaker {
35 
36     private final CameraCommandExecutor mCameraCommandExecutor;
37     private final FrameServer mFrameServer;
38     private final ManagedImageReader mImageFactory;
39     private final RequestBuilder.Factory mRequestBuilder;
40     private final Surface mBurstInputSurface;
41     private final Runnable mRestorePreviewCommand;
42     private final int mMaxImageCount;
43     /**
44      * The lifetime of the burst, the burst stops capturing images once the
45      * lifetime is closed
46      */
47     @Nullable
48     private Lifetime mBurstLifetime;
49 
50     /**
51      * Creates an instance for burst taker.
52      * <p/>
53      *
54      * @param cameraCommandExecutor the executor to use for executing
55      *            {@link BurstCaptureCommand}
56      * @param frameServer the {@link FrameServer} instance for creating session
57      * @param builder factory to use for creating the {@link Request} for burst
58      *            capture
59      * @param imageFactory the factory to use for creating a stream of images
60      * @param burstInputSurface the input surface to use for streaming preview
61      *            frames to burst
62      * @param restorePreviewCommand the command to run to restore the preview,
63      *            once burst capture is complete
64      * @param maxImageCount the maximum number of images supported by the image
65      *            reader
66      */
BurstTakerImpl(CameraCommandExecutor cameraCommandExecutor, FrameServer frameServer, RequestBuilder.Factory builder, ManagedImageReader imageFactory, Surface burstInputSurface, Runnable restorePreviewCommand, int maxImageCount)67     public BurstTakerImpl(CameraCommandExecutor cameraCommandExecutor,
68             FrameServer frameServer, RequestBuilder.Factory builder,
69             ManagedImageReader imageFactory, Surface burstInputSurface,
70             Runnable restorePreviewCommand,
71             int maxImageCount) {
72         mCameraCommandExecutor = cameraCommandExecutor;
73         mFrameServer = frameServer;
74         mRequestBuilder = builder;
75         mImageFactory = imageFactory;
76         mBurstInputSurface = burstInputSurface;
77         mRestorePreviewCommand = restorePreviewCommand;
78         mMaxImageCount = maxImageCount;
79     }
80 
81     @Override
startBurst(EvictionHandler evictionHandler, BurstController burstController)82     public void startBurst(EvictionHandler evictionHandler,
83             BurstController burstController) {
84         MainThread.checkMainThread();
85         Preconditions.checkState(mBurstLifetime == null,
86                 "Burst cannot be started, while another is running.");
87         mBurstLifetime = new Lifetime();
88         BurstCaptureCommand burstCommand = new BurstCaptureCommand(
89                 mFrameServer, mRequestBuilder,
90                 mImageFactory, mBurstInputSurface,
91                 mBurstLifetime, evictionHandler, burstController, mRestorePreviewCommand,
92                 mMaxImageCount);
93 
94         mCameraCommandExecutor.execute(burstCommand);
95     }
96 
97     @Override
stopBurst()98     public synchronized void stopBurst() {
99         MainThread.checkMainThread();
100         if (mBurstLifetime != null) {
101             mBurstLifetime.close();
102             mBurstLifetime = null;
103         }
104     }
105 }
106