ICameraRecordingProxy.cpp revision 6773d4777f4ccbbe6377e4ae1b42c117066ae6ba
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 "ICameraRecordingProxy"
19#include <camera/CameraUtils.h>
20#include <camera/ICameraRecordingProxy.h>
21#include <camera/ICameraRecordingProxyListener.h>
22#include <binder/IMemory.h>
23#include <binder/Parcel.h>
24#include <media/hardware/HardwareAPI.h>
25#include <stdint.h>
26#include <utils/Log.h>
27
28namespace android {
29
30enum {
31    START_RECORDING = IBinder::FIRST_CALL_TRANSACTION,
32    STOP_RECORDING,
33    RELEASE_RECORDING_FRAME,
34};
35
36
37class BpCameraRecordingProxy: public BpInterface<ICameraRecordingProxy>
38{
39public:
40    BpCameraRecordingProxy(const sp<IBinder>& impl)
41        : BpInterface<ICameraRecordingProxy>(impl)
42    {
43    }
44
45    status_t startRecording(const sp<ICameraRecordingProxyListener>& listener)
46    {
47        ALOGV("startRecording");
48        Parcel data, reply;
49        data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
50        data.writeStrongBinder(IInterface::asBinder(listener));
51        remote()->transact(START_RECORDING, data, &reply);
52        return reply.readInt32();
53    }
54
55    void stopRecording()
56    {
57        ALOGV("stopRecording");
58        Parcel data, reply;
59        data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
60        remote()->transact(STOP_RECORDING, data, &reply);
61    }
62
63    void releaseRecordingFrame(const sp<IMemory>& mem)
64    {
65        ALOGV("releaseRecordingFrame");
66        Parcel data, reply;
67        data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
68        data.writeStrongBinder(IInterface::asBinder(mem));
69
70        native_handle_t *nh = nullptr;
71        if (CameraUtils::isNativeHandleMetadata(mem)) {
72            VideoNativeHandleMetadata *metadata =
73                        (VideoNativeHandleMetadata*)(mem->pointer());
74            nh = metadata->pHandle;
75            data.writeNativeHandle(nh);
76        }
77
78        remote()->transact(RELEASE_RECORDING_FRAME, data, &reply);
79
80        if (nh) {
81            // Close the native handle because camera received a dup copy.
82            native_handle_close(nh);
83            native_handle_delete(nh);
84        }
85    }
86};
87
88IMPLEMENT_META_INTERFACE(CameraRecordingProxy, "android.hardware.ICameraRecordingProxy");
89
90// ----------------------------------------------------------------------
91
92status_t BnCameraRecordingProxy::onTransact(
93    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
94{
95    switch(code) {
96        case START_RECORDING: {
97            ALOGV("START_RECORDING");
98            CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
99            sp<ICameraRecordingProxyListener> listener =
100                interface_cast<ICameraRecordingProxyListener>(data.readStrongBinder());
101            reply->writeInt32(startRecording(listener));
102            return NO_ERROR;
103        } break;
104        case STOP_RECORDING: {
105            ALOGV("STOP_RECORDING");
106            CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
107            stopRecording();
108            return NO_ERROR;
109        } break;
110        case RELEASE_RECORDING_FRAME: {
111            ALOGV("RELEASE_RECORDING_FRAME");
112            CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
113            sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder());
114
115            if (CameraUtils::isNativeHandleMetadata(mem)) {
116                VideoNativeHandleMetadata *metadata =
117                        (VideoNativeHandleMetadata*)(mem->pointer());
118                metadata->pHandle = data.readNativeHandle();
119
120                // releaseRecordingFrame will be responsble to close the native handle.
121            }
122            releaseRecordingFrame(mem);
123
124            return NO_ERROR;
125        } break;
126
127        default:
128            return BBinder::onTransact(code, data, reply, flags);
129    }
130}
131
132// ----------------------------------------------------------------------------
133
134}; // namespace android
135
136