blob: bd6af75c0a142828b231200f2656b5d5e5ff6092 [file] [log] [blame]
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +08001/*
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"
Praveen Chavan6773d472016-01-13 01:24:30 -080019#include <camera/CameraUtils.h>
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080020#include <camera/ICameraRecordingProxy.h>
21#include <camera/ICameraRecordingProxyListener.h>
22#include <binder/IMemory.h>
23#include <binder/Parcel.h>
Praveen Chavan6773d472016-01-13 01:24:30 -080024#include <media/hardware/HardwareAPI.h>
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080025#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,
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -070034 RELEASE_RECORDING_FRAME_HANDLE,
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070035 RELEASE_RECORDING_FRAME_HANDLE_BATCH,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080036};
37
38
39class BpCameraRecordingProxy: public BpInterface<ICameraRecordingProxy>
40{
41public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070042 explicit BpCameraRecordingProxy(const sp<IBinder>& impl)
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080043 : BpInterface<ICameraRecordingProxy>(impl)
44 {
45 }
46
47 status_t startRecording(const sp<ICameraRecordingProxyListener>& listener)
48 {
Steve Block3856b092011-10-20 11:56:00 +010049 ALOGV("startRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080050 Parcel data, reply;
51 data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
Marco Nelissen06b46062014-11-14 07:58:25 -080052 data.writeStrongBinder(IInterface::asBinder(listener));
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080053 remote()->transact(START_RECORDING, data, &reply);
54 return reply.readInt32();
55 }
56
57 void stopRecording()
58 {
Steve Block3856b092011-10-20 11:56:00 +010059 ALOGV("stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080060 Parcel data, reply;
61 data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
62 remote()->transact(STOP_RECORDING, data, &reply);
63 }
64
65 void releaseRecordingFrame(const sp<IMemory>& mem)
66 {
Steve Block3856b092011-10-20 11:56:00 +010067 ALOGV("releaseRecordingFrame");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080068 Parcel data, reply;
69 data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
Marco Nelissen06b46062014-11-14 07:58:25 -080070 data.writeStrongBinder(IInterface::asBinder(mem));
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080071 remote()->transact(RELEASE_RECORDING_FRAME, data, &reply);
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -070072 }
Praveen Chavan6773d472016-01-13 01:24:30 -080073
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -070074 void releaseRecordingFrameHandle(native_handle_t *handle) {
75 ALOGV("releaseRecordingFrameHandle");
76 Parcel data, reply;
77 data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
78 data.writeNativeHandle(handle);
79
80 remote()->transact(RELEASE_RECORDING_FRAME_HANDLE, data, &reply);
81
82 // Close the native handle because camera received a dup copy.
83 native_handle_close(handle);
84 native_handle_delete(handle);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080085 }
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070086
87 void releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles) {
88 ALOGV("releaseRecordingFrameHandleBatch");
89 Parcel data, reply;
90 data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
91 uint32_t n = handles.size();
92 data.writeUint32(n);
93 for (auto& handle : handles) {
94 data.writeNativeHandle(handle);
95 }
96 remote()->transact(RELEASE_RECORDING_FRAME_HANDLE_BATCH, data, &reply);
97
98 // Close the native handle because camera received a dup copy.
99 for (auto& handle : handles) {
100 native_handle_close(handle);
101 native_handle_delete(handle);
102 }
103 }
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800104};
105
106IMPLEMENT_META_INTERFACE(CameraRecordingProxy, "android.hardware.ICameraRecordingProxy");
107
108// ----------------------------------------------------------------------
109
110status_t BnCameraRecordingProxy::onTransact(
111 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
112{
113 switch(code) {
114 case START_RECORDING: {
Steve Block3856b092011-10-20 11:56:00 +0100115 ALOGV("START_RECORDING");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800116 CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
117 sp<ICameraRecordingProxyListener> listener =
118 interface_cast<ICameraRecordingProxyListener>(data.readStrongBinder());
119 reply->writeInt32(startRecording(listener));
120 return NO_ERROR;
121 } break;
122 case STOP_RECORDING: {
Steve Block3856b092011-10-20 11:56:00 +0100123 ALOGV("STOP_RECORDING");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800124 CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
125 stopRecording();
126 return NO_ERROR;
127 } break;
128 case RELEASE_RECORDING_FRAME: {
Steve Block3856b092011-10-20 11:56:00 +0100129 ALOGV("RELEASE_RECORDING_FRAME");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800130 CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
131 sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder());
132 releaseRecordingFrame(mem);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800133 return NO_ERROR;
134 } break;
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700135 case RELEASE_RECORDING_FRAME_HANDLE: {
136 ALOGV("RELEASE_RECORDING_FRAME_HANDLE");
137 CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800138
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700139 // releaseRecordingFrameHandle will be responsble to close the native handle.
140 releaseRecordingFrameHandle(data.readNativeHandle());
141 return NO_ERROR;
142 } break;
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700143 case RELEASE_RECORDING_FRAME_HANDLE_BATCH: {
144 ALOGV("RELEASE_RECORDING_FRAME_HANDLE_BATCH");
145 CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
146 uint32_t n = 0;
147 status_t res = data.readUint32(&n);
148 if (res != OK) {
149 ALOGE("%s: Failed to read batch size: %s (%d)", __FUNCTION__, strerror(-res), res);
150 return BAD_VALUE;
151 }
152 std::vector<native_handle_t*> handles;
153 handles.reserve(n);
154 for (uint32_t i = 0; i < n; i++) {
155 native_handle_t* handle = data.readNativeHandle();
156 if (handle == nullptr) {
157 ALOGE("%s: Received a null native handle at handles[%d]",
158 __FUNCTION__, i);
159 return BAD_VALUE;
160 }
161 handles.push_back(handle);
162 }
163
164 // releaseRecordingFrameHandleBatch will be responsble to close the native handle.
165 releaseRecordingFrameHandleBatch(handles);
166 return NO_ERROR;
167 } break;
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800168 default:
169 return BBinder::onTransact(code, data, reply, flags);
170 }
171}
172
173// ----------------------------------------------------------------------------
174
175}; // namespace android
176