CameraDeviceClient.h revision 412fe56cd7cf7d73bc5d2bcc3f635bc650d18de9
1e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin/*
2e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * Copyright (C) 2013 The Android Open Source Project
3e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin *
4e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * Licensed under the Apache License, Version 2.0 (the "License");
5e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * you may not use this file except in compliance with the License.
6e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * You may obtain a copy of the License at
7e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin *
8e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin *      http://www.apache.org/licenses/LICENSE-2.0
9e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin *
10e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * Unless required by applicable law or agreed to in writing, software
11e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * distributed under the License is distributed on an "AS IS" BASIS,
12e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * See the License for the specific language governing permissions and
14e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * limitations under the License.
15e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin */
16e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
17e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin#ifndef ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
18e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin#define ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
19e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
207b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include <camera/camera2/ICameraDeviceUser.h>
217b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include <camera/camera2/ICameraDeviceCallbacks.h>
22b97babb8c08969b55af3b6456d15f764c8873d3fYin-Chia Yeh#include <camera/camera2/OutputConfiguration.h>
237b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala
24e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin#include "CameraService.h"
257b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "common/FrameProcessorBase.h"
267b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "common/Camera2ClientBase.h"
27e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
28e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkinnamespace android {
29e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
3077a77f6ef6f04dba8caa54bda937cb276cace9adRuben Brunkstruct CameraDeviceClientBase : public CameraService::BasicClient, public BnCameraDeviceUser
31e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin{
32e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    typedef ICameraDeviceCallbacks TCamCallbacks;
33e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
34e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    const sp<ICameraDeviceCallbacks>& getRemoteCallback() {
35e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin        return mRemoteCallback;
36e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    }
37e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
38e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkinprotected:
39e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    CameraDeviceClientBase(const sp<CameraService>& cameraService,
40e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            const sp<ICameraDeviceCallbacks>& remoteCallback,
41e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            const String16& clientPackageName,
42e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            int cameraId,
43e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            int cameraFacing,
44e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            int clientPid,
45e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            uid_t clientUid,
46e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            int servicePid);
47e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
48e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    sp<ICameraDeviceCallbacks> mRemoteCallback;
49e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin};
50e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
51e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin/**
52e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * Implements the binder ICameraDeviceUser API,
53e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * meant for HAL3-public implementation of
54e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin * android.hardware.photography.CameraDevice
55e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin */
56e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkinclass CameraDeviceClient :
57e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin        public Camera2ClientBase<CameraDeviceClientBase>,
587b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala        public camera2::FrameProcessorBase::FilteredListener
59e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin{
60e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkinpublic:
61e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    /**
62e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin     * ICameraDeviceUser interface (see ICameraDeviceUser for details)
63e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin     */
64e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
65e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    // Note that the callee gets a copy of the metadata.
6690e59c98c343e941b1a75307ffa4b4b5f1eb50d6Jianing Wei    virtual status_t           submitRequest(sp<CaptureRequest> request,
67cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                             bool streaming = false,
68cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                             /*out*/
69cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                             int64_t* lastFrameNumber = NULL);
7090e59c98c343e941b1a75307ffa4b4b5f1eb50d6Jianing Wei    // List of requests are copied.
7190e59c98c343e941b1a75307ffa4b4b5f1eb50d6Jianing Wei    virtual status_t           submitRequestList(List<sp<CaptureRequest> > requests,
72cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                                 bool streaming = false,
73cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                                 /*out*/
74cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                                 int64_t* lastFrameNumber = NULL);
75cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei    virtual status_t      cancelRequest(int requestId,
76cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                        /*out*/
77cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                        int64_t* lastFrameNumber = NULL);
78e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
79b2119af7f4ced0ecfefd4c7388f86b4e3a3ea7d8Ruben Brunk    virtual status_t beginConfigure();
80b2119af7f4ced0ecfefd4c7388f86b4e3a3ea7d8Ruben Brunk
811fa8999c91d5df81949aa723000058380cd3faa2Zhijun He    virtual status_t endConfigure(bool isConstrainedHighSpeed = false);
82b2119af7f4ced0ecfefd4c7388f86b4e3a3ea7d8Ruben Brunk
83e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    // Returns -EBUSY if device is not idle
84e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    virtual status_t      deleteStream(int streamId);
85e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
86b97babb8c08969b55af3b6456d15f764c8873d3fYin-Chia Yeh    virtual status_t      createStream(const OutputConfiguration &outputConfiguration);
87e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
88618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    // Create an input stream of width, height, and format.
89618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    virtual status_t      createInputStream(int width, int height, int format);
90618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen
91618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    // Get the buffer producer of the input stream
92618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    virtual status_t      getInputBufferProducer(
93618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen                                /*out*/sp<IGraphicBufferProducer> *producer);
94618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen
95e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    // Create a request object from a template.
96e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    virtual status_t      createDefaultRequest(int templateId,
97e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin                                               /*out*/
98e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin                                               CameraMetadata* request);
99e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
100e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    // Get the static metadata for the camera
101e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    // -- Caller owns the newly allocated metadata
102099b457f3203fa51387e21bd450495abb973ab31Igor Murashkin    virtual status_t      getCameraInfo(/*out*/CameraMetadata* info);
103e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
1042ab500c632569e2f131a1a2288459933da70c4eeZhijun He    // Wait until all the submitted requests have finished processing
1052ab500c632569e2f131a1a2288459933da70c4eeZhijun He    virtual status_t      waitUntilIdle();
106abaa51d3ca31f0eda99e1d271e6dc64c877dbf58Eino-Ville Talvala
107abaa51d3ca31f0eda99e1d271e6dc64c877dbf58Eino-Ville Talvala    // Flush all active and pending requests as fast as possible
108cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei    virtual status_t      flush(/*out*/
109cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                                int64_t* lastFrameNumber = NULL);
110abaa51d3ca31f0eda99e1d271e6dc64c877dbf58Eino-Ville Talvala
1114d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    // Prepare stream by preallocating its buffers
1124d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    virtual status_t      prepare(int streamId);
1134d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
114b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala    // Tear down stream resources by freeing its unused buffers
115b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala    virtual status_t      tearDown(int streamId);
116b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala
117c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk    // Prepare stream by preallocating up to maxCount of its buffers
118c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk    virtual status_t      prepare2(int maxCount, int streamId);
119c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk
120e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    /**
121e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin     * Interface used by CameraService
122e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin     */
123e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
124e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    CameraDeviceClient(const sp<CameraService>& cameraService,
125e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            const sp<ICameraDeviceCallbacks>& remoteCallback,
126e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            const String16& clientPackageName,
127e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            int cameraId,
128e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            int cameraFacing,
129e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            int clientPid,
130e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            uid_t clientUid,
131e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin            int servicePid);
132e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    virtual ~CameraDeviceClient();
133e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
134e074a93046ebe5cea0b55c3a479e082a426e1e07Yin-Chia Yeh    virtual status_t      initialize(CameraModule *module);
135e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
136e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    virtual status_t      dump(int fd, const Vector<String16>& args);
137e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
138e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    /**
139f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala     * Device listener interface
140f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala     */
141f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala
142f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala    virtual void notifyIdle();
143cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei    virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
144cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei                             const CaptureResultExtras& resultExtras);
145cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei    virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
1464d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    virtual void notifyPrepared(int streamId);
147f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala
148f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala    /**
149e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin     * Interface used by independent components of CameraDeviceClient.
150e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin     */
151e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkinprotected:
152e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    /** FilteredListener implementation **/
153cb0652e5a850b2fcd919e977247e87239efaf70eJianing Wei    virtual void          onResultAvailable(const CaptureResult& result);
154e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    virtual void          detachDevice();
155e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
156f8b2a6f7dea06234c7966798d9363d2d236488a6Igor Murashkin    // Calculate the ANativeWindow transform from android.sensor.orientation
157f8b2a6f7dea06234c7966798d9363d2d236488a6Igor Murashkin    status_t              getRotationTransformLocked(/*out*/int32_t* transform);
158f8b2a6f7dea06234c7966798d9363d2d236488a6Igor Murashkin
159e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkinprivate:
160e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    /** ICameraDeviceUser interface-related private members */
161e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
162e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    /** Preview callback related members */
1637b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala    sp<camera2::FrameProcessorBase> mFrameProcessor;
164e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
165e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
166e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
167e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    /** Utility members */
168e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    bool enforceRequestPermissions(CameraMetadata& metadata);
169e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
170bba75576c3bc5a90cd8e14bd053ab2d74a6c7e9dRuben Brunk    // Find the square of the euclidean distance between two points
171bba75576c3bc5a90cd8e14bd053ab2d74a6c7e9dRuben Brunk    static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
172bba75576c3bc5a90cd8e14bd053ab2d74a6c7e9dRuben Brunk
173bba75576c3bc5a90cd8e14bd053ab2d74a6c7e9dRuben Brunk    // Find the closest dimensions for a given format in available stream configurations with
174bba75576c3bc5a90cd8e14bd053ab2d74a6c7e9dRuben Brunk    // a width <= ROUNDING_WIDTH_CAP
17577a77f6ef6f04dba8caa54bda937cb276cace9adRuben Brunk    static const int32_t ROUNDING_WIDTH_CAP = 1920;
176bba75576c3bc5a90cd8e14bd053ab2d74a6c7e9dRuben Brunk    static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
1773d82c0d9ed2b3e956ad699a9ca2c8a70c9d24678Eino-Ville Talvala            android_dataspace dataSpace, const CameraMetadata& info,
1783d82c0d9ed2b3e956ad699a9ca2c8a70c9d24678Eino-Ville Talvala            /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
179bba75576c3bc5a90cd8e14bd053ab2d74a6c7e9dRuben Brunk
180618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    // IGraphicsBufferProducer binder -> Stream ID for output streams
181e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    KeyedVector<sp<IBinder>, int> mStreamMap;
182e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
183618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    struct InputStreamConfiguration {
184618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen        bool configured;
185618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen        int32_t width;
186618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen        int32_t height;
187618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen        int32_t format;
188618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen        int32_t id;
189618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    } mInputStream;
190618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen
191618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    // Request ID
192e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    Vector<int> mStreamingRequestList;
193e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
194e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin    int32_t mRequestIdCounter;
195412fe56cd7cf7d73bc5d2bcc3f635bc650d18de9Eino-Ville Talvala
196e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin};
197e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
198e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin}; // namespace android
199e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin
200e7ee7637747371635a85fedd24d2190bb1f38651Igor Murashkin#endif
201