ICameraService.cpp revision b84d935c179a275a47e07291d2a983daf844de80
1/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <binder/Parcel.h>
22#include <binder/IPCThreadState.h>
23#include <binder/IServiceManager.h>
24
25#include <camera/ICameraService.h>
26#include <camera/IProCameraUser.h>
27#include <camera/IProCameraCallbacks.h>
28#include <camera/ICamera.h>
29#include <camera/ICameraClient.h>
30
31namespace android {
32
33class BpCameraService: public BpInterface<ICameraService>
34{
35public:
36    BpCameraService(const sp<IBinder>& impl)
37        : BpInterface<ICameraService>(impl)
38    {
39    }
40
41    // get number of cameras available
42    virtual int32_t getNumberOfCameras()
43    {
44        Parcel data, reply;
45        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
46        remote()->transact(BnCameraService::GET_NUMBER_OF_CAMERAS, data, &reply);
47        return reply.readInt32();
48    }
49
50    // get information about a camera
51    virtual status_t getCameraInfo(int cameraId,
52                                   struct CameraInfo* cameraInfo) {
53        Parcel data, reply;
54        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
55        data.writeInt32(cameraId);
56        remote()->transact(BnCameraService::GET_CAMERA_INFO, data, &reply);
57        cameraInfo->facing = reply.readInt32();
58        cameraInfo->orientation = reply.readInt32();
59        return reply.readInt32();
60    }
61
62    // connect to camera service
63    virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId,
64                                const String16 &clientPackageName, int clientUid)
65    {
66        Parcel data, reply;
67        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
68        data.writeStrongBinder(cameraClient->asBinder());
69        data.writeInt32(cameraId);
70        data.writeString16(clientPackageName);
71        data.writeInt32(clientUid);
72        remote()->transact(BnCameraService::CONNECT, data, &reply);
73        return interface_cast<ICamera>(reply.readStrongBinder());
74    }
75
76    // connect to camera service (pro client)
77    virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb, int cameraId,
78                                       const String16 &clientPackageName, int clientUid)
79    {
80        Parcel data, reply;
81        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
82        data.writeStrongBinder(cameraCb->asBinder());
83        data.writeInt32(cameraId);
84        data.writeString16(clientPackageName);
85        data.writeInt32(clientUid);
86        remote()->transact(BnCameraService::CONNECT_PRO, data, &reply);
87        return interface_cast<IProCameraUser>(reply.readStrongBinder());
88    }
89};
90
91IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService");
92
93// ----------------------------------------------------------------------
94
95status_t BnCameraService::onTransact(
96    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
97{
98    switch(code) {
99        case GET_NUMBER_OF_CAMERAS: {
100            CHECK_INTERFACE(ICameraService, data, reply);
101            reply->writeInt32(getNumberOfCameras());
102            return NO_ERROR;
103        } break;
104        case GET_CAMERA_INFO: {
105            CHECK_INTERFACE(ICameraService, data, reply);
106            CameraInfo cameraInfo;
107            memset(&cameraInfo, 0, sizeof(cameraInfo));
108            status_t result = getCameraInfo(data.readInt32(), &cameraInfo);
109            reply->writeInt32(cameraInfo.facing);
110            reply->writeInt32(cameraInfo.orientation);
111            reply->writeInt32(result);
112            return NO_ERROR;
113        } break;
114        case CONNECT: {
115            CHECK_INTERFACE(ICameraService, data, reply);
116            sp<ICameraClient> cameraClient =
117                    interface_cast<ICameraClient>(data.readStrongBinder());
118            int32_t cameraId = data.readInt32();
119            const String16 clientName = data.readString16();
120            int32_t clientUid = data.readInt32();
121            sp<ICamera> camera = connect(cameraClient, cameraId,
122                    clientName, clientUid);
123            reply->writeStrongBinder(camera->asBinder());
124            return NO_ERROR;
125        } break;
126        case CONNECT_PRO: {
127            CHECK_INTERFACE(ICameraService, data, reply);
128            sp<IProCameraCallbacks> cameraClient = interface_cast<IProCameraCallbacks>(data.readStrongBinder());
129            int32_t cameraId = data.readInt32();
130            const String16 clientName = data.readString16();
131            int32_t clientUid = data.readInt32();
132            sp<IProCameraUser> camera = connect(cameraClient, cameraId,
133                                                clientName, clientUid);
134            reply->writeStrongBinder(camera->asBinder());
135            return NO_ERROR;
136        } break;
137        default:
138            return BBinder::onTransact(code, data, reply, flags);
139    }
140}
141
142// ----------------------------------------------------------------------------
143
144}; // namespace android
145