• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 #ifndef ANDROID_SERVERS_CAMERA_CAMERACLIENT_H
18 #define ANDROID_SERVERS_CAMERA_CAMERACLIENT_H
19 
20 #include "CameraService.h"
21 
22 namespace android {
23 
24 class MemoryHeapBase;
25 class CameraHardwareInterface;
26 
27 /**
28  * Interface between android.hardware.Camera API and Camera HAL device for version
29  * CAMERA_DEVICE_API_VERSION_1_0.
30  */
31 
32 class CameraClient : public CameraService::Client
33 {
34 public:
35     // ICamera interface (see ICamera for details)
36     virtual binder::Status  disconnect();
37     virtual status_t        connect(const sp<hardware::ICameraClient>& client);
38     virtual status_t        lock();
39     virtual status_t        unlock();
40     virtual status_t        setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer);
41     virtual void            setPreviewCallbackFlag(int flag);
42     virtual status_t        setPreviewCallbackTarget(
43             const sp<IGraphicBufferProducer>& callbackProducer);
44     virtual status_t        startPreview();
45     virtual void            stopPreview();
46     virtual bool            previewEnabled();
47     virtual status_t        setVideoBufferMode(int32_t videoBufferMode);
48     virtual status_t        startRecording();
49     virtual void            stopRecording();
50     virtual bool            recordingEnabled();
51     virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
52     virtual void            releaseRecordingFrameHandle(native_handle_t *handle);
53     virtual void            releaseRecordingFrameHandleBatch(
54                                     const std::vector<native_handle_t*>& handles);
55     virtual status_t        autoFocus();
56     virtual status_t        cancelAutoFocus();
57     virtual status_t        takePicture(int msgType);
58     virtual status_t        setParameters(const String8& params);
59     virtual String8         getParameters() const;
60     virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
61     virtual status_t        setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer);
62 
63     // Interface used by CameraService
64     CameraClient(const sp<CameraService>& cameraService,
65             const sp<hardware::ICameraClient>& cameraClient,
66             const String16& clientPackageName,
67             int cameraId,
68             int cameraFacing,
69             int clientPid,
70             int clientUid,
71             int servicePid,
72             bool legacyMode = false);
73     ~CameraClient();
74 
75     virtual status_t initialize(sp<CameraProviderManager> manager,
76             const String8& monitorTags) override;
77 
78     virtual status_t dump(int fd, const Vector<String16>& args);
79 
80     virtual status_t dumpClient(int fd, const Vector<String16>& args);
81 
82 private:
83 
84     // check whether the calling process matches mClientPid.
85     status_t                checkPid() const;
86     status_t                checkPidAndHardware() const;  // also check mHardware != 0
87 
88     // these are internal functions used to set up preview buffers
89     status_t                registerPreviewBuffers();
90 
91     // camera operation mode
92     enum camera_mode {
93         CAMERA_PREVIEW_MODE   = 0,  // frame automatically released
94         CAMERA_RECORDING_MODE = 1,  // frame has to be explicitly released by releaseRecordingFrame()
95     };
96     // these are internal functions used for preview/recording
97     status_t                startCameraMode(camera_mode mode);
98     status_t                startPreviewMode();
99     status_t                startRecordingMode();
100 
101     // internal function used by sendCommand to enable/disable shutter sound.
102     status_t                enableShutterSound(bool enable);
103 
104     static sp<CameraClient>        getClientFromCookie(void* user);
105 
106     // these are static callback functions
107     static void             notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
108     static void             dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
109             camera_frame_metadata_t *metadata, void* user);
110     static void             dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
111     static void             handleCallbackTimestampBatch(
112                                     int32_t msgType, const std::vector<HandleTimestampMessage>&, void* user);
113     // handlers for messages
114     void                    handleShutter(void);
115     void                    handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
116             camera_frame_metadata_t *metadata);
117     void                    handlePostview(const sp<IMemory>& mem);
118     void                    handleRawPicture(const sp<IMemory>& mem);
119     void                    handleCompressedPicture(const sp<IMemory>& mem);
120     void                    handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
121     void                    handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
122             camera_frame_metadata_t *metadata);
123     void                    handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
124 
125     void                    copyFrameAndPostCopiedFrame(
126         int32_t msgType,
127         const sp<hardware::ICameraClient>& client,
128         const sp<IMemoryHeap>& heap,
129         size_t offset, size_t size,
130         camera_frame_metadata_t *metadata);
131 
132     int                     getOrientation(int orientation, bool mirror);
133 
134     status_t                setPreviewWindow(
135         const sp<IBinder>& binder,
136         const sp<ANativeWindow>& window);
137 
138 
139     // these are initialized in the constructor.
140     sp<CameraHardwareInterface>     mHardware;       // cleared after disconnect()
141     int                             mPreviewCallbackFlag;
142     int                             mOrientation;     // Current display orientation
143     bool                            mPlayShutterSound;
144     bool                            mLegacyMode; // camera2 api legacy mode?
145 
146     // Ensures atomicity among the public methods
147     mutable Mutex                   mLock;
148     // This is a binder of Surface or Surface.
149     sp<IBinder>                     mSurface;
150     sp<ANativeWindow>               mPreviewWindow;
151 
152     // If the user want us to return a copy of the preview frame (instead
153     // of the original one), we allocate mPreviewBuffer and reuse it if possible.
154     sp<MemoryHeapBase>              mPreviewBuffer;
155 
156     // Debugging information
157     CameraParameters                mLatestSetParameters;
158 
159     // mAvailableCallbackBuffers stores sp<IMemory> that HAL uses to send VideoNativeHandleMetadata.
160     // It will be used to send VideoNativeHandleMetadata back to HAL when camera receives the
161     // native handle from releaseRecordingFrameHandle.
162     Mutex                           mAvailableCallbackBuffersLock;
163     std::vector<sp<IMemory>>        mAvailableCallbackBuffers;
164 
165     // We need to avoid the deadlock when the incoming command thread and
166     // the CameraHardwareInterface callback thread both want to grab mLock.
167     // An extra flag is used to tell the callback thread that it should stop
168     // trying to deliver the callback messages if the client is not
169     // interested in it anymore. For example, if the client is calling
170     // stopPreview(), the preview frame messages do not need to be delivered
171     // anymore.
172 
173     // This function takes the same parameter as the enableMsgType() and
174     // disableMsgType() functions in CameraHardwareInterface.
175     void                    enableMsgType(int32_t msgType);
176     void                    disableMsgType(int32_t msgType);
177     volatile int32_t        mMsgEnabled;
178 
179     // This function keeps trying to grab mLock, or give up if the message
180     // is found to be disabled. It returns true if mLock is grabbed.
181     bool                    lockIfMessageWanted(int32_t msgType);
182 };
183 
184 }
185 
186 #endif
187