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 <android/hardware/ICameraClient.h>
25#include <media/hardware/HardwareAPI.h>
26
27namespace android {
28namespace hardware {
29
30enum {
31    NOTIFY_CALLBACK = IBinder::FIRST_CALL_TRANSACTION,
32    DATA_CALLBACK,
33    DATA_CALLBACK_TIMESTAMP,
34    RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP,
35};
36
37class BpCameraClient: public BpInterface<ICameraClient>
38{
39public:
40    BpCameraClient(const sp<IBinder>& impl)
41        : BpInterface<ICameraClient>(impl)
42    {
43    }
44
45    // generic callback from camera service to app
46    void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
47    {
48        ALOGV("notifyCallback");
49        Parcel data, reply;
50        data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
51        data.writeInt32(msgType);
52        data.writeInt32(ext1);
53        data.writeInt32(ext2);
54        remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
55    }
56
57    // generic data callback from camera service to app with image data
58    void dataCallback(int32_t msgType, const sp<IMemory>& imageData,
59                      camera_frame_metadata_t *metadata)
60    {
61        ALOGV("dataCallback");
62        Parcel data, reply;
63        data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
64        data.writeInt32(msgType);
65        data.writeStrongBinder(IInterface::asBinder(imageData));
66        if (metadata) {
67            data.writeInt32(metadata->number_of_faces);
68            data.write(metadata->faces, sizeof(camera_face_t) * metadata->number_of_faces);
69        }
70        remote()->transact(DATA_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
71    }
72
73    // generic data callback from camera service to app with image data
74    void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData)
75    {
76        ALOGV("dataCallback");
77        Parcel data, reply;
78        data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
79        data.writeInt64(timestamp);
80        data.writeInt32(msgType);
81        data.writeStrongBinder(IInterface::asBinder(imageData));
82        remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY);
83    }
84
85    void recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle) {
86        ALOGV("recordingFrameHandleCallbackTimestamp");
87        Parcel data, reply;
88        data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
89        data.writeInt64(timestamp);
90        data.writeNativeHandle(handle);
91        remote()->transact(RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP, data, &reply,
92                IBinder::FLAG_ONEWAY);
93    }
94};
95
96IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient");
97
98// ----------------------------------------------------------------------
99
100status_t BnCameraClient::onTransact(
101    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
102{
103    switch(code) {
104        case NOTIFY_CALLBACK: {
105            ALOGV("NOTIFY_CALLBACK");
106            CHECK_INTERFACE(ICameraClient, data, reply);
107            int32_t msgType = data.readInt32();
108            int32_t ext1 = data.readInt32();
109            int32_t ext2 = data.readInt32();
110            notifyCallback(msgType, ext1, ext2);
111            return NO_ERROR;
112        } break;
113        case DATA_CALLBACK: {
114            ALOGV("DATA_CALLBACK");
115            CHECK_INTERFACE(ICameraClient, data, reply);
116            int32_t msgType = data.readInt32();
117            sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
118            camera_frame_metadata_t *metadata = NULL;
119            if (data.dataAvail() > 0) {
120                metadata = new camera_frame_metadata_t;
121                metadata->number_of_faces = data.readInt32();
122                metadata->faces = (camera_face_t *) data.readInplace(
123                        sizeof(camera_face_t) * metadata->number_of_faces);
124            }
125            dataCallback(msgType, imageData, metadata);
126            if (metadata) delete metadata;
127            return NO_ERROR;
128        } break;
129        case DATA_CALLBACK_TIMESTAMP: {
130            ALOGV("DATA_CALLBACK_TIMESTAMP");
131            CHECK_INTERFACE(ICameraClient, data, reply);
132            nsecs_t timestamp = data.readInt64();
133            int32_t msgType = data.readInt32();
134            sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
135            dataCallbackTimestamp(timestamp, msgType, imageData);
136            return NO_ERROR;
137        } break;
138        case RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP: {
139            ALOGV("RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP");
140            CHECK_INTERFACE(ICameraClient, data, reply);
141            nsecs_t timestamp;
142            status_t res = data.readInt64(&timestamp);
143            if (res != OK) {
144                ALOGE("%s: Failed to read timestamp: %s (%d)", __FUNCTION__, strerror(-res), res);
145                return BAD_VALUE;
146            }
147            native_handle_t* handle = data.readNativeHandle();
148            if (handle == nullptr) {
149                ALOGE("%s: Received a null native handle", __FUNCTION__);
150                return BAD_VALUE;
151            }
152
153            // The native handle will be freed in BpCamera::releaseRecordingFrameHandle.
154            recordingFrameHandleCallbackTimestamp(timestamp, handle);
155            return NO_ERROR;
156        } break;
157        default:
158            return BBinder::onTransact(code, data, reply, flags);
159    }
160}
161
162// ----------------------------------------------------------------------------
163
164} // namespace hardware
165} // namespace android
166