1/*
2 * Copyright (C) 2011 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//#define LOG_NDEBUG 0
18#define LOG_TAG "ICameraRecordingProxyListener"
19#include <camera/CameraUtils.h>
20#include <camera/ICameraRecordingProxyListener.h>
21#include <binder/IMemory.h>
22#include <binder/Parcel.h>
23#include <media/hardware/HardwareAPI.h>
24#include <utils/Log.h>
25
26namespace android {
27
28enum {
29    DATA_CALLBACK_TIMESTAMP = IBinder::FIRST_CALL_TRANSACTION,
30    RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP,
31};
32
33class BpCameraRecordingProxyListener: public BpInterface<ICameraRecordingProxyListener>
34{
35public:
36    BpCameraRecordingProxyListener(const sp<IBinder>& impl)
37        : BpInterface<ICameraRecordingProxyListener>(impl)
38    {
39    }
40
41    void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData)
42    {
43        ALOGV("dataCallback");
44        Parcel data, reply;
45        data.writeInterfaceToken(ICameraRecordingProxyListener::getInterfaceDescriptor());
46        data.writeInt64(timestamp);
47        data.writeInt32(msgType);
48        data.writeStrongBinder(IInterface::asBinder(imageData));
49        remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY);
50    }
51
52    void recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle) {
53        ALOGV("recordingFrameHandleCallbackTimestamp");
54        Parcel data, reply;
55        data.writeInterfaceToken(ICameraRecordingProxyListener::getInterfaceDescriptor());
56        data.writeInt64(timestamp);
57        data.writeNativeHandle(handle);
58        remote()->transact(RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP, data, &reply,
59                IBinder::FLAG_ONEWAY);
60
61        // The native handle is dupped in ICameraClient so we need to free it here.
62        native_handle_close(handle);
63        native_handle_delete(handle);
64    }
65};
66
67IMPLEMENT_META_INTERFACE(CameraRecordingProxyListener, "android.hardware.ICameraRecordingProxyListener");
68
69// ----------------------------------------------------------------------
70
71status_t BnCameraRecordingProxyListener::onTransact(
72    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
73{
74    switch(code) {
75        case DATA_CALLBACK_TIMESTAMP: {
76            ALOGV("DATA_CALLBACK_TIMESTAMP");
77            CHECK_INTERFACE(ICameraRecordingProxyListener, data, reply);
78            nsecs_t timestamp = data.readInt64();
79            int32_t msgType = data.readInt32();
80            sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
81            dataCallbackTimestamp(timestamp, msgType, imageData);
82            return NO_ERROR;
83        } break;
84        case RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP: {
85            ALOGV("RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP");
86            CHECK_INTERFACE(ICameraRecordingProxyListener, data, reply);
87            nsecs_t timestamp;
88            status_t res = data.readInt64(&timestamp);
89            if (res != OK) {
90                ALOGE("%s: Failed to read timestamp: %s (%d)", __FUNCTION__, strerror(-res), res);
91                return BAD_VALUE;
92            }
93
94            native_handle_t* handle = data.readNativeHandle();
95            if (handle == nullptr) {
96                ALOGE("%s: Received a null native handle", __FUNCTION__);
97                return BAD_VALUE;
98            }
99            // The native handle will be freed in
100            // BpCameraRecordingProxy::releaseRecordingFrameHandle.
101            recordingFrameHandleCallbackTimestamp(timestamp, handle);
102            return NO_ERROR;
103        } break;
104        default:
105            return BBinder::onTransact(code, data, reply, flags);
106    }
107}
108
109// ----------------------------------------------------------------------------
110
111}; // namespace android
112
113