1 /*
2  * Copyright (C) 2016 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 android.hardware.camera2.impl;
18 
19 import static android.hardware.camera2.CameraAccessException.CAMERA_DISABLED;
20 import static android.hardware.camera2.CameraAccessException.CAMERA_DISCONNECTED;
21 import static android.hardware.camera2.CameraAccessException.CAMERA_IN_USE;
22 import static android.hardware.camera2.CameraAccessException.CAMERA_ERROR;
23 import static android.hardware.camera2.CameraAccessException.MAX_CAMERAS_IN_USE;
24 import static android.hardware.camera2.CameraAccessException.CAMERA_DEPRECATED_HAL;
25 
26 import android.hardware.ICameraService;
27 import android.hardware.camera2.CameraManager;
28 import android.hardware.camera2.CameraAccessException;
29 import android.hardware.camera2.CaptureRequest;
30 import android.hardware.camera2.ICameraDeviceUser;
31 import android.hardware.camera2.ICameraDeviceCallbacks;
32 import android.hardware.camera2.ICameraOfflineSession;
33 import android.hardware.camera2.impl.CameraMetadataNative;
34 import android.hardware.camera2.params.OutputConfiguration;
35 import android.hardware.camera2.params.SessionConfiguration;
36 import android.hardware.camera2.utils.SubmitInfo;
37 import android.os.IBinder;
38 import android.os.RemoteException;
39 import android.os.ServiceSpecificException;
40 import android.view.Surface;
41 
42 /**
43  * A wrapper around ICameraDeviceUser.
44  *
45  * Mainly used to convert ServiceSpecificExceptions to the correct
46  * checked / unchecked exception.
47  *
48  * @hide
49  */
50 public class ICameraDeviceUserWrapper {
51 
52     private final ICameraDeviceUser mRemoteDevice;
53 
ICameraDeviceUserWrapper(ICameraDeviceUser remoteDevice)54     public ICameraDeviceUserWrapper(ICameraDeviceUser remoteDevice) {
55         if (remoteDevice == null) {
56             throw new NullPointerException("Remote device may not be null");
57         }
58         mRemoteDevice = remoteDevice;
59     }
60 
unlinkToDeath(IBinder.DeathRecipient recipient, int flags)61     public void unlinkToDeath(IBinder.DeathRecipient recipient, int flags) {
62         if (mRemoteDevice.asBinder() != null) {
63             mRemoteDevice.asBinder().unlinkToDeath(recipient, flags);
64         }
65     }
66 
disconnect()67     public void disconnect()  {
68         try {
69             mRemoteDevice.disconnect();
70         } catch (RemoteException t) {
71             // ignore binder errors for disconnect
72         }
73     }
74 
submitRequest(CaptureRequest request, boolean streaming)75     public SubmitInfo submitRequest(CaptureRequest request, boolean streaming)
76             throws CameraAccessException  {
77         try {
78             return mRemoteDevice.submitRequest(request, streaming);
79         } catch (Throwable t) {
80             CameraManager.throwAsPublicException(t);
81             throw new UnsupportedOperationException("Unexpected exception", t);
82         }
83     }
84 
submitRequestList(CaptureRequest[] requestList, boolean streaming)85     public SubmitInfo submitRequestList(CaptureRequest[] requestList, boolean streaming)
86             throws CameraAccessException {
87         try {
88             return mRemoteDevice.submitRequestList(requestList, streaming);
89         } catch (Throwable t) {
90             CameraManager.throwAsPublicException(t);
91             throw new UnsupportedOperationException("Unexpected exception", t);
92         }
93     }
94 
cancelRequest(int requestId)95     public long cancelRequest(int requestId) throws CameraAccessException {
96         try {
97             return mRemoteDevice.cancelRequest(requestId);
98         } catch (Throwable t) {
99             CameraManager.throwAsPublicException(t);
100             throw new UnsupportedOperationException("Unexpected exception", t);
101         }
102     }
103 
beginConfigure()104     public void beginConfigure() throws CameraAccessException {
105         try {
106             mRemoteDevice.beginConfigure();
107         } catch (Throwable t) {
108             CameraManager.throwAsPublicException(t);
109             throw new UnsupportedOperationException("Unexpected exception", t);
110         }
111     }
112 
endConfigure(int operatingMode, CameraMetadataNative sessionParams, long startTimeMs)113     public int[] endConfigure(int operatingMode, CameraMetadataNative sessionParams,
114             long startTimeMs) throws CameraAccessException {
115         try {
116             return mRemoteDevice.endConfigure(operatingMode, (sessionParams == null) ?
117                     new CameraMetadataNative() : sessionParams, startTimeMs);
118         } catch (Throwable t) {
119             CameraManager.throwAsPublicException(t);
120             throw new UnsupportedOperationException("Unexpected exception", t);
121         }
122     }
123 
deleteStream(int streamId)124     public void deleteStream(int streamId) throws CameraAccessException {
125         try {
126             mRemoteDevice.deleteStream(streamId);
127         } catch (Throwable t) {
128             CameraManager.throwAsPublicException(t);
129             throw new UnsupportedOperationException("Unexpected exception", t);
130         }
131     }
132 
createStream(OutputConfiguration outputConfiguration)133     public int createStream(OutputConfiguration outputConfiguration)
134             throws CameraAccessException {
135         try {
136             return mRemoteDevice.createStream(outputConfiguration);
137         } catch (Throwable t) {
138             CameraManager.throwAsPublicException(t);
139             throw new UnsupportedOperationException("Unexpected exception", t);
140         }
141     }
142 
createInputStream(int width, int height, int format, boolean isMultiResolution)143     public int createInputStream(int width, int height, int format, boolean isMultiResolution)
144             throws CameraAccessException {
145         try {
146             return mRemoteDevice.createInputStream(width, height, format, isMultiResolution);
147         } catch (Throwable t) {
148             CameraManager.throwAsPublicException(t);
149             throw new UnsupportedOperationException("Unexpected exception", t);
150         }
151     }
152 
getInputSurface()153     public Surface getInputSurface() throws CameraAccessException {
154         try {
155             return mRemoteDevice.getInputSurface();
156         } catch (Throwable t) {
157             CameraManager.throwAsPublicException(t);
158             throw new UnsupportedOperationException("Unexpected exception", t);
159         }
160     }
161 
createDefaultRequest(int templateId)162     public CameraMetadataNative createDefaultRequest(int templateId) throws CameraAccessException {
163         try {
164             return mRemoteDevice.createDefaultRequest(templateId);
165         } catch (Throwable t) {
166             CameraManager.throwAsPublicException(t);
167             throw new UnsupportedOperationException("Unexpected exception", t);
168         }
169     }
170 
getCameraInfo()171     public CameraMetadataNative getCameraInfo() throws CameraAccessException {
172         try {
173             return mRemoteDevice.getCameraInfo();
174         } catch (Throwable t) {
175             CameraManager.throwAsPublicException(t);
176             throw new UnsupportedOperationException("Unexpected exception", t);
177         }
178     }
179 
waitUntilIdle()180     public void waitUntilIdle() throws CameraAccessException {
181         try {
182             mRemoteDevice.waitUntilIdle();
183         } catch (Throwable t) {
184             CameraManager.throwAsPublicException(t);
185             throw new UnsupportedOperationException("Unexpected exception", t);
186         }
187     }
188 
isSessionConfigurationSupported(SessionConfiguration sessionConfig)189     public boolean isSessionConfigurationSupported(SessionConfiguration sessionConfig)
190             throws CameraAccessException {
191         try {
192             return mRemoteDevice.isSessionConfigurationSupported(sessionConfig);
193         } catch (ServiceSpecificException e) {
194             if (e.errorCode == ICameraService.ERROR_INVALID_OPERATION) {
195                 throw new UnsupportedOperationException("Session configuration query not " +
196                         "supported");
197             } else if (e.errorCode == ICameraService.ERROR_ILLEGAL_ARGUMENT) {
198                 throw new IllegalArgumentException("Invalid session configuration");
199             }
200 
201             throw e;
202         } catch (Throwable t) {
203             CameraManager.throwAsPublicException(t);
204             throw new UnsupportedOperationException("Unexpected exception", t);
205         }
206     }
207 
flush()208     public long flush() throws CameraAccessException {
209         try {
210             return mRemoteDevice.flush();
211         } catch (Throwable t) {
212             CameraManager.throwAsPublicException(t);
213             throw new UnsupportedOperationException("Unexpected exception", t);
214         }
215     }
216 
prepare(int streamId)217     public void prepare(int streamId) throws CameraAccessException {
218         try {
219             mRemoteDevice.prepare(streamId);
220         } catch (Throwable t) {
221             CameraManager.throwAsPublicException(t);
222             throw new UnsupportedOperationException("Unexpected exception", t);
223         }
224     }
225 
tearDown(int streamId)226     public void tearDown(int streamId) throws CameraAccessException {
227         try {
228             mRemoteDevice.tearDown(streamId);
229         } catch (Throwable t) {
230             CameraManager.throwAsPublicException(t);
231             throw new UnsupportedOperationException("Unexpected exception", t);
232         }
233     }
234 
prepare2(int maxCount, int streamId)235     public void prepare2(int maxCount, int streamId) throws CameraAccessException {
236         try {
237             mRemoteDevice.prepare2(maxCount, streamId);
238         } catch (Throwable t) {
239             CameraManager.throwAsPublicException(t);
240             throw new UnsupportedOperationException("Unexpected exception", t);
241         }
242     }
243 
updateOutputConfiguration(int streamId, OutputConfiguration config)244     public void updateOutputConfiguration(int streamId, OutputConfiguration config)
245             throws CameraAccessException {
246         try {
247             mRemoteDevice.updateOutputConfiguration(streamId, config);
248         } catch (Throwable t) {
249             CameraManager.throwAsPublicException(t);
250             throw new UnsupportedOperationException("Unexpected exception", t);
251         }
252     }
253 
switchToOffline(ICameraDeviceCallbacks cbs, int[] offlineOutputIds)254     public ICameraOfflineSession switchToOffline(ICameraDeviceCallbacks cbs,
255             int[] offlineOutputIds) throws CameraAccessException {
256         try {
257             return mRemoteDevice.switchToOffline(cbs, offlineOutputIds);
258         } catch (Throwable t) {
259             CameraManager.throwAsPublicException(t);
260             throw new UnsupportedOperationException("Unexpected exception", t);
261         }
262     }
263 
finalizeOutputConfigurations(int streamId, OutputConfiguration deferredConfig)264     public void finalizeOutputConfigurations(int streamId, OutputConfiguration deferredConfig)
265             throws CameraAccessException {
266         try {
267             mRemoteDevice.finalizeOutputConfigurations(streamId, deferredConfig);
268         } catch (Throwable t) {
269             CameraManager.throwAsPublicException(t);
270             throw new UnsupportedOperationException("Unexpected exception", t);
271         }
272     }
273 
setCameraAudioRestriction(int mode)274     public void setCameraAudioRestriction(int mode) throws CameraAccessException {
275         try {
276             mRemoteDevice.setCameraAudioRestriction(mode);
277         } catch (Throwable t) {
278             CameraManager.throwAsPublicException(t);
279             throw new UnsupportedOperationException("Unexpected exception", t);
280         }
281     }
282 
getGlobalAudioRestriction()283     public int getGlobalAudioRestriction() throws CameraAccessException {
284         try {
285             return mRemoteDevice.getGlobalAudioRestriction();
286         } catch (Throwable t) {
287             CameraManager.throwAsPublicException(t);
288             throw new UnsupportedOperationException("Unexpected exception", t);
289         }
290     }
291 
292 }
293