ICameraClient.cpp revision 6773d4777f4ccbbe6377e4ae1b42c117066ae6ba
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//#define LOG_NDEBUG 0
19#define LOG_TAG "ICameraClient"
20#include <utils/Log.h>
21#include <stdint.h>
22#include <sys/types.h>
23#include <camera/CameraUtils.h>
24#include <camera/ICameraClient.h>
25#include <media/hardware/HardwareAPI.h>
26
27namespace android {
28
29enum {
30    NOTIFY_CALLBACK = IBinder::FIRST_CALL_TRANSACTION,
31    DATA_CALLBACK,
32    DATA_CALLBACK_TIMESTAMP,
33};
34
35class BpCameraClient: public BpInterface<ICameraClient>
36{
37public:
38    BpCameraClient(const sp<IBinder>& impl)
39        : BpInterface<ICameraClient>(impl)
40    {
41    }
42
43    // generic callback from camera service to app
44    void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
45    {
46        ALOGV("notifyCallback");
47        Parcel data, reply;
48        data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
49        data.writeInt32(msgType);
50        data.writeInt32(ext1);
51        data.writeInt32(ext2);
52        remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
53    }
54
55    // generic data callback from camera service to app with image data
56    void dataCallback(int32_t msgType, const sp<IMemory>& imageData,
57                      camera_frame_metadata_t *metadata)
58    {
59        ALOGV("dataCallback");
60        Parcel data, reply;
61        data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
62        data.writeInt32(msgType);
63        data.writeStrongBinder(IInterface::asBinder(imageData));
64        if (metadata) {
65            data.writeInt32(metadata->number_of_faces);
66            data.write(metadata->faces, sizeof(camera_face_t) * metadata->number_of_faces);
67        }
68        remote()->transact(DATA_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
69    }
70
71    // generic data callback from camera service to app with image data
72    void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData)
73    {
74        ALOGV("dataCallback");
75        Parcel data, reply;
76        data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
77        data.writeInt64(timestamp);
78        data.writeInt32(msgType);
79        data.writeStrongBinder(IInterface::asBinder(imageData));
80        // If imageData is metadata and it contains a native handle, write the native handle to
81        // parcel.
82        if (CameraUtils::isNativeHandleMetadata(imageData)) {
83            VideoNativeHandleMetadata *metadata =
84                    (VideoNativeHandleMetadata*)(imageData->pointer());
85            data.writeNativeHandle(metadata->pHandle);
86        }
87        remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY);
88    }
89};
90
91IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient");
92
93// ----------------------------------------------------------------------
94
95status_t BnCameraClient::onTransact(
96    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
97{
98    switch(code) {
99        case NOTIFY_CALLBACK: {
100            ALOGV("NOTIFY_CALLBACK");
101            CHECK_INTERFACE(ICameraClient, data, reply);
102            int32_t msgType = data.readInt32();
103            int32_t ext1 = data.readInt32();
104            int32_t ext2 = data.readInt32();
105            notifyCallback(msgType, ext1, ext2);
106            return NO_ERROR;
107        } break;
108        case DATA_CALLBACK: {
109            ALOGV("DATA_CALLBACK");
110            CHECK_INTERFACE(ICameraClient, data, reply);
111            int32_t msgType = data.readInt32();
112            sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
113            camera_frame_metadata_t *metadata = NULL;
114            if (data.dataAvail() > 0) {
115                metadata = new camera_frame_metadata_t;
116                metadata->number_of_faces = data.readInt32();
117                metadata->faces = (camera_face_t *) data.readInplace(
118                        sizeof(camera_face_t) * metadata->number_of_faces);
119            }
120            dataCallback(msgType, imageData, metadata);
121            if (metadata) delete metadata;
122            return NO_ERROR;
123        } break;
124        case DATA_CALLBACK_TIMESTAMP: {
125            ALOGV("DATA_CALLBACK_TIMESTAMP");
126            CHECK_INTERFACE(ICameraClient, data, reply);
127            nsecs_t timestamp = data.readInt64();
128            int32_t msgType = data.readInt32();
129            sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
130
131            // If the image data contains a native handle, read the native handle from the parcel
132            // and replace the native handle in the image data. (The native handle in image data is
133            // not serielized/deserialized so it's not valid in the process.)
134            if (CameraUtils::isNativeHandleMetadata(imageData)) {
135                VideoNativeHandleMetadata *metadata =
136                        (VideoNativeHandleMetadata*)(imageData->pointer());
137                metadata->pHandle = data.readNativeHandle();
138
139                // The native handle will be freed in
140                // BpCameraRecordingProxyListener::releaseRecordingFrame.
141            }
142
143            dataCallbackTimestamp(timestamp, msgType, imageData);
144            return NO_ERROR;
145        } break;
146        default:
147            return BBinder::onTransact(code, data, reply, flags);
148    }
149}
150
151// ----------------------------------------------------------------------------
152
153}; // namespace android
154
155