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.sharedimagereader;
18 
19 import android.view.Surface;
20 
21 import com.android.camera.async.BufferQueue;
22 import com.android.camera.async.BufferQueueController;
23 import com.android.camera.async.ForwardingBufferQueue;
24 import com.android.camera.async.Lifetime;
25 import com.android.camera.one.v2.camera2proxy.ImageProxy;
26 import com.android.camera.one.v2.core.ResourceAcquisitionFailedException;
27 import com.android.camera.one.v2.sharedimagereader.imagedistributor.ImageDistributor;
28 import com.android.camera.one.v2.sharedimagereader.imagedistributor.ImageStream;
29 
30 /**
31  * An ImageQueueCaptureStream which registers itself with the ImageDistributor
32  * when bound to begin receiving images.
33  */
34 class ImageStreamImpl extends ForwardingBufferQueue<ImageProxy> implements
35         ImageStream {
36     private final ImageDistributor mImageDistributor;
37     private final BufferQueueController<ImageProxy> mImageStreamController;
38     private final Surface mSurface;
39     private final Lifetime mLifetime;
40 
ImageStreamImpl(BufferQueue<ImageProxy> imageStream, BufferQueueController<ImageProxy> imageStreamController, ImageDistributor imageDistributor, Surface surface)41     public ImageStreamImpl(BufferQueue<ImageProxy> imageStream,
42             BufferQueueController<ImageProxy> imageStreamController,
43             ImageDistributor imageDistributor, Surface surface) {
44         super(imageStream);
45         mSurface = surface;
46         mImageDistributor = imageDistributor;
47         mImageStreamController = imageStreamController;
48         mLifetime = new Lifetime();
49         mLifetime.add(imageStream);
50         mLifetime.add(mImageStreamController);
51     }
52 
53     @Override
bind(BufferQueue<Long> timestamps)54     public Surface bind(BufferQueue<Long> timestamps) throws InterruptedException,
55             ResourceAcquisitionFailedException {
56         mLifetime.add(timestamps);
57         mImageDistributor.addRoute(timestamps, mImageStreamController);
58         return mSurface;
59     }
60 
61     @Override
close()62     public void close() {
63         mLifetime.close();
64     }
65 }
66