1/*
2**
3** Copyright 2013, 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 "ICameraDeviceCallbacks"
20#include <utils/Log.h>
21#include <stdint.h>
22#include <sys/types.h>
23
24#include <binder/Parcel.h>
25#include <gui/IGraphicBufferProducer.h>
26#include <gui/Surface.h>
27#include <utils/Mutex.h>
28
29#include <camera/camera2/ICameraDeviceCallbacks.h>
30#include "camera/CameraMetadata.h"
31#include "camera/CaptureResult.h"
32
33namespace android {
34
35enum {
36    CAMERA_ERROR = IBinder::FIRST_CALL_TRANSACTION,
37    CAMERA_IDLE,
38    CAPTURE_STARTED,
39    RESULT_RECEIVED,
40    PREPARED
41};
42
43class BpCameraDeviceCallbacks: public BpInterface<ICameraDeviceCallbacks>
44{
45public:
46    BpCameraDeviceCallbacks(const sp<IBinder>& impl)
47        : BpInterface<ICameraDeviceCallbacks>(impl)
48    {
49    }
50
51    void onDeviceError(CameraErrorCode errorCode, const CaptureResultExtras& resultExtras)
52    {
53        ALOGV("onDeviceError");
54        Parcel data, reply;
55        data.writeInterfaceToken(ICameraDeviceCallbacks::getInterfaceDescriptor());
56        data.writeInt32(static_cast<int32_t>(errorCode));
57        data.writeInt32(1); // to mark presence of CaptureResultExtras object
58        resultExtras.writeToParcel(&data);
59        remote()->transact(CAMERA_ERROR, data, &reply, IBinder::FLAG_ONEWAY);
60        data.writeNoException();
61    }
62
63    void onDeviceIdle()
64    {
65        ALOGV("onDeviceIdle");
66        Parcel data, reply;
67        data.writeInterfaceToken(ICameraDeviceCallbacks::getInterfaceDescriptor());
68        remote()->transact(CAMERA_IDLE, data, &reply, IBinder::FLAG_ONEWAY);
69        data.writeNoException();
70    }
71
72    void onCaptureStarted(const CaptureResultExtras& result, int64_t timestamp)
73    {
74        ALOGV("onCaptureStarted");
75        Parcel data, reply;
76        data.writeInterfaceToken(ICameraDeviceCallbacks::getInterfaceDescriptor());
77        data.writeInt32(1); // to mark presence of CaptureResultExtras object
78        result.writeToParcel(&data);
79        data.writeInt64(timestamp);
80        remote()->transact(CAPTURE_STARTED, data, &reply, IBinder::FLAG_ONEWAY);
81        data.writeNoException();
82    }
83
84    void onResultReceived(const CameraMetadata& metadata,
85            const CaptureResultExtras& resultExtras) {
86        ALOGV("onResultReceived");
87        Parcel data, reply;
88        data.writeInterfaceToken(ICameraDeviceCallbacks::getInterfaceDescriptor());
89        data.writeInt32(1); // to mark presence of metadata object
90        metadata.writeToParcel(&data);
91        data.writeInt32(1); // to mark presence of CaptureResult object
92        resultExtras.writeToParcel(&data);
93        remote()->transact(RESULT_RECEIVED, data, &reply, IBinder::FLAG_ONEWAY);
94        data.writeNoException();
95    }
96
97    void onPrepared(int streamId)
98    {
99        ALOGV("onPrepared");
100        Parcel data, reply;
101        data.writeInterfaceToken(ICameraDeviceCallbacks::getInterfaceDescriptor());
102        data.writeInt32(streamId);
103        remote()->transact(PREPARED, data, &reply, IBinder::FLAG_ONEWAY);
104        data.writeNoException();
105    }
106
107};
108
109IMPLEMENT_META_INTERFACE(CameraDeviceCallbacks,
110                         "android.hardware.camera2.ICameraDeviceCallbacks");
111
112// ----------------------------------------------------------------------
113
114status_t BnCameraDeviceCallbacks::onTransact(
115    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
116{
117    ALOGV("onTransact - code = %d", code);
118    switch(code) {
119        case CAMERA_ERROR: {
120            ALOGV("onDeviceError");
121            CHECK_INTERFACE(ICameraDeviceCallbacks, data, reply);
122            CameraErrorCode errorCode =
123                    static_cast<CameraErrorCode>(data.readInt32());
124            CaptureResultExtras resultExtras;
125            if (data.readInt32() != 0) {
126                resultExtras.readFromParcel(const_cast<Parcel*>(&data));
127            } else {
128                ALOGE("No CaptureResultExtras object is present!");
129            }
130            onDeviceError(errorCode, resultExtras);
131            data.readExceptionCode();
132            return NO_ERROR;
133        } break;
134        case CAMERA_IDLE: {
135            ALOGV("onDeviceIdle");
136            CHECK_INTERFACE(ICameraDeviceCallbacks, data, reply);
137            onDeviceIdle();
138            data.readExceptionCode();
139            return NO_ERROR;
140        } break;
141        case CAPTURE_STARTED: {
142            ALOGV("onCaptureStarted");
143            CHECK_INTERFACE(ICameraDeviceCallbacks, data, reply);
144            CaptureResultExtras result;
145            if (data.readInt32() != 0) {
146                result.readFromParcel(const_cast<Parcel*>(&data));
147            } else {
148                ALOGE("No CaptureResultExtras object is present in result!");
149            }
150            int64_t timestamp = data.readInt64();
151            onCaptureStarted(result, timestamp);
152            data.readExceptionCode();
153            return NO_ERROR;
154        } break;
155        case RESULT_RECEIVED: {
156            ALOGV("onResultReceived");
157            CHECK_INTERFACE(ICameraDeviceCallbacks, data, reply);
158            CameraMetadata metadata;
159            if (data.readInt32() != 0) {
160                metadata.readFromParcel(const_cast<Parcel*>(&data));
161            } else {
162                ALOGW("No metadata object is present in result");
163            }
164            CaptureResultExtras resultExtras;
165            if (data.readInt32() != 0) {
166                resultExtras.readFromParcel(const_cast<Parcel*>(&data));
167            } else {
168                ALOGW("No capture result extras object is present in result");
169            }
170            onResultReceived(metadata, resultExtras);
171            data.readExceptionCode();
172            return NO_ERROR;
173        } break;
174        case PREPARED: {
175            ALOGV("onPrepared");
176            CHECK_INTERFACE(ICameraDeviceCallbacks, data, reply);
177            CaptureResultExtras result;
178            int streamId = data.readInt32();
179            onPrepared(streamId);
180            data.readExceptionCode();
181            return NO_ERROR;
182        } break;
183        default:
184            return BBinder::onTransact(code, data, reply, flags);
185    }
186}
187
188// ----------------------------------------------------------------------------
189
190}; // namespace android
191