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.imagedistributor;
18 
19 import android.os.Handler;
20 
21 import com.android.camera.async.ConcurrentBufferQueue;
22 import com.android.camera.async.HandlerFactory;
23 import com.android.camera.async.Lifetime;
24 import com.android.camera.async.Updatable;
25 import com.android.camera.debug.Loggers;
26 import com.android.camera.one.v2.camera2proxy.ImageReaderProxy;
27 
28 public class ImageDistributorFactory {
29     private final ImageDistributorImpl mImageDistributor;
30     private final Updatable<Long> mTimestampStream;
31 
32     /**
33      * Creates an ImageDistributor from the given ImageReader.
34      *
35      * @param lifetime The lifetime of the image distributor. Images will stop
36      *            being distributed when the lifetime closes.
37      * @param imageReader The ImageReader from which to distribute images.
38      * @param handlerFactory Used for creating handler threads for callbacks
39      *            registered with the platform.
40      */
ImageDistributorFactory(Lifetime lifetime, ImageReaderProxy imageReader, HandlerFactory handlerFactory)41     public ImageDistributorFactory(Lifetime lifetime, ImageReaderProxy imageReader,
42             HandlerFactory handlerFactory) {
43         ConcurrentBufferQueue<Long> globalTimestampStream = new ConcurrentBufferQueue<>();
44         mTimestampStream = globalTimestampStream;
45         lifetime.add(globalTimestampStream);
46         mImageDistributor = new ImageDistributorImpl(Loggers.tagFactory(), globalTimestampStream);
47 
48         // This imageReaderHandler will be created with a very very high thread
49         // priority because missing any input event potentially stalls the
50         // camera preview and HAL.
51         Handler imageReaderHandler = handlerFactory.create(lifetime, "ImageDistributor",
52               Thread.MAX_PRIORITY);
53 
54         imageReader.setOnImageAvailableListener(
55                 new ImageDistributorOnImageAvailableListener(imageReader, mImageDistributor),
56                 imageReaderHandler);
57     }
58 
provideImageDistributor()59     public ImageDistributor provideImageDistributor() {
60         return mImageDistributor;
61     }
62 
provideGlobalTimestampCallback()63     public Updatable<Long> provideGlobalTimestampCallback() {
64         return mTimestampStream;
65     }
66 }
67