summaryrefslogtreecommitdiffstats
path: root/test/VTC/CameraHardwareInterfaceTest.cpp
blob: dd01a3605a9d4b066ef09857726e374c179492ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */


#define LOG_NDEBUG 0
#define LOG_TAG "CamHalTest"

#include <fcntl.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <semaphore.h>
#include <pthread.h>


#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>

#include <camera/Camera.h>
#include <camera/ICamera.h>

#include <cutils/log.h>
#include <media/stagefright/CameraSource.h>
#include <media/stagefright/MetaData.h>

#ifdef ANDROID_API_JB_OR_LATER
#include <gui/Surface.h>
#include <gui/ISurface.h>
#include <gui/ISurfaceComposer.h>
#include <gui/ISurfaceComposerClient.h>
#include <gui/SurfaceComposerClient.h>
#else
#include <surfaceflinger/Surface.h>
#include <surfaceflinger/ISurface.h>
#include <surfaceflinger/ISurfaceComposer.h>
#include <surfaceflinger/ISurfaceComposerClient.h>
#include <surfaceflinger/SurfaceComposerClient.h>
#endif

#include <cutils/properties.h>

#include <hardware/camera.h>
#include <hardware/hardware.h>
#include "CameraHardwareInterface.h"

#include "VtcCommon.h"

using namespace android;

int startPreviewNow = 1;
int mWidth = 640;
int mHeight = 480;

static int cameraId = 0;
static int mPriority = 0;
camera_module_t *mModule;
sp<CameraHardwareInterface> mHardware = NULL;

static int surface_setup_complete = 0;
sp<Surface> mSurface;
sp<SurfaceComposerClient> mComposerClient;
sp<SurfaceControl> mSurfaceControl;
sp<ANativeWindow> mWindow;

static pthread_cond_t mCond;
static pthread_mutex_t mMutex;


void my_notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user) {
    VTC_LOGD("\n\nnotifyCallback(%d)\n\n", msgType);
}

void my_dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
    VTC_LOGD("\n\ndataCallback(%d)\n\n", msgType);
}

void my_dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
    VTC_LOGD("\n\ndataCallbackTimestamp(%d)\n\n", msgType);
}

void surfaceInit() {
    status_t retval;

    mComposerClient = new SurfaceComposerClient;

    retval = mComposerClient->initCheck();
    if (retval != NO_ERROR) {
        VTC_LOGE("mCOmposerClient->initCheck failed");
        return;
    }

    mSurfaceControl = mComposerClient->createSurface(String8("NativeSurface"),
            0, mWidth, mHeight,
            PIXEL_FORMAT_OPAQUE, ISurfaceComposer::eFXSurfaceNormal);

    if (mSurfaceControl == NULL) {
        VTC_LOGE("mComposerClient->createSurface failed");
        return;
    }

    if (mSurfaceControl->isValid()) {
        VTC_LOGE("mSurfaceControl is valid");
    } else {
        VTC_LOGE("mSurfaceControl is not valid");
        return;
    }

    SurfaceComposerClient::openGlobalTransaction();

    retval =  mSurfaceControl->setPosition(100,100);
    if (retval != NO_ERROR) {
        VTC_LOGE("mCOmposerClient->setPosition failed");
        return;
    }

    retval =  mSurfaceControl->setSize(400,400);
    if (retval != NO_ERROR) {
        VTC_LOGE("mCOmposerClient->setPosition failed");
        return;
    }

    retval =  mSurfaceControl->setLayer(999990);
    if (retval != NO_ERROR) {
        VTC_LOGE("mCOmposerClient->setLayer 999990 failed");
        return;
    }

    retval = mSurfaceControl->show();
    if (retval != NO_ERROR) {
        VTC_LOGE("mCOmposerClient->show failed");
        return;
    }
    SurfaceComposerClient::closeGlobalTransaction();

    mSurface = mSurfaceControl->getSurface();
    if (mSurface == NULL) {
        VTC_LOGE("mSurfaceControl->getSurface failed");
        return;
    }

    surface_setup_complete = 1;
}

int main (int argc, char* argv[]) {
    status_t result;
    char cmd[160],*cmdptr;
    struct camera_info info;
    char camera_device_name[10];

    sp<ProcessState> proc(ProcessState::self());
    ProcessState::self()->startThreadPool();

    if (argc >= 2) {
        mPriority = atoi(argv[1]);
    }

    if (argc == 3) {
        startPreviewNow = atoi(argv[2]);
    }

    if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
            (const hw_module_t **)&mModule) < 0) {
        VTC_LOGE("Could not load camera HAL module");
        return -1;
    }
    VTC_LOGD("\nLoaded the camera module\n");


    if (mModule->get_camera_info(cameraId, &info) != OK) {
        VTC_LOGE("Invalid camera id %d", cameraId);
        return -1;
    }
    VTC_LOGD("\nLoaded the camera properties\n");

    if (startPreviewNow == 0) return -1;

    snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
    mHardware = new CameraHardwareInterface(camera_device_name);
    if (mHardware->initialize(&mModule->common) != OK) {
        mHardware.clear();
        VTC_LOGE("mHardware->initialize FAILED");
        return -1;
    }
    VTC_LOGD("\nInitialized the camera hardware\n");

    mHardware->setCallbacks(my_notifyCallback,
            my_dataCallback,
            my_dataCallbackTimestamp,
            (void *)cameraId);

    mHardware->enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_PREVIEW_METADATA);

    String8 param_str(mHardware->getParameters().flatten());
    CameraParameters params(param_str);
    params.setPreviewSize(mWidth, mHeight);
    params.set("priority", mPriority);
    //params.dump();
    mHardware->setParameters(params);

    surfaceInit();
    if (surface_setup_complete == 0) {
        VTC_LOGE("\n\nsurfaceInit failed! \n\n");
        goto EXIT;
    }

    mWindow = mSurface;
    if (mWindow == 0) {
        VTC_LOGE("\n\nWhy is mWindow == 0?? \n\n");
        goto EXIT;
    }

    result = native_window_api_connect(mWindow.get(), NATIVE_WINDOW_API_CAMERA);
    if (result != NO_ERROR) {
        VTC_LOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
                result);
        goto EXIT;
    }

    native_window_set_scaling_mode(mWindow.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
    native_window_set_buffers_transform(mWindow.get(), 0);

    result = mHardware->setPreviewWindow(mWindow);
    if (result != NO_ERROR) {
        VTC_LOGE("mHardware->setPreviewWindow");
        goto EXIT;
    }

    mHardware->startPreview();

#if USE_TIMED_WAIT
    pthread_mutex_init(&mMutex, NULL);
    pthread_cond_init(&mCond, NULL);

    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    ts.tv_sec += 30;
    pthread_mutex_lock(&mMutex);
    pthread_cond_timedwait(&mCond, &mMutex, &ts);
    pthread_mutex_unlock(&mMutex);
#else

    while (1) {
        cmdptr = fgets(cmd, sizeof(cmd), stdin);
        if (!strncmp(cmdptr,"quit",4)) {
            break;
        }
    }
#endif
    VTC_LOGD("\n\n STOPPING PREVIEW \n\n");
    mHardware->stopPreview();


EXIT:
    if (mHardware != 0){
        mHardware->release();

        if (mWindow != 0) {
            result = native_window_api_disconnect(mWindow.get(), NATIVE_WINDOW_API_CAMERA);
            if (result != NO_ERROR) {
                VTC_LOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result), result);
            }
            mWindow = 0;
        }
        mHardware.clear();
    }

    if ( NULL != mSurface.get() ) {
        mSurface.clear();
    }

    if ( NULL != mSurfaceControl.get() ) {
        mSurfaceControl->clear();
        mSurfaceControl.clear();
    }

    if ( NULL != mComposerClient.get() ) {
        mComposerClient->dispose();
        mComposerClient.clear();
    }

#if USE_TIMED_WAIT
    pthread_mutex_destroy(&mMutex);
    pthread_cond_destroy(&mCond);
#endif

    VTC_LOGD("\n\n SUCCESS \n\n");

    return 0;
}