#include "TerminalDevice.h" /* * Copyright 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #define LOG_TAG "CameraTestHelpers" #include "PhoneDevice.h" #include #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) CPhoneDevice::CPhoneDevice() { } CPhoneDevice::~CPhoneDevice() { } IDevice::timer_uid_t CPhoneDevice::registerTimer(unsigned int timerType, unsigned int timeout) { return 0; } bool CPhoneDevice::unregisterTimer(IDevice::timer_uid_t uid) { return true; } bool CPhoneDevice::onTimeout(IDevice::timer_uid_t uid, unsigned int timerType, unsigned int times) { return true; } bool CPhoneDevice::TakePhoto(unsigned char channel, unsigned char preset, const string& path, bool photo) { int cameraId = (int)channel - 1; ACameraIdList *cameraIdList = NULL; ACameraMetadata *cameraMetadata = NULL; const char *selectedCameraId = NULL; camera_status_t camera_status = ACAMERA_OK; ACameraManager *cameraManager = ACameraManager_create(); camera_status = ACameraManager_getCameraIdList(cameraManager, &cameraIdList); if (camera_status != ACAMERA_OK) { LOGI("Failed to get camera id list (reason: %d)\n", camera_status); return false; } if (cameraIdList->numCameras < 1 ) { LOGI("No camera device detected.\n"); return false; } if (cameraIdList->numCameras <= cameraId ) { LOGI("No required camera device %d detected.\n", cameraId); return false; } selectedCameraId = cameraIdList->cameraIds[cameraId]; LOGI("Trying to open Camera2 (id: %s, num of camera : %d)\n", selectedCameraId, cameraIdList->numCameras); camera_status = ACameraManager_getCameraCharacteristics(cameraManager, selectedCameraId, &cameraMetadata); if (camera_status != ACAMERA_OK) { LOGI("Failed to get camera meta data of ID:%s\n", selectedCameraId); } deviceStateCallbacks.onDisconnected = camera_device_on_disconnected; deviceStateCallbacks.onError = camera_device_on_error; camera_status = ACameraManager_openCamera(cameraManager, selectedCameraId, &deviceStateCallbacks, &cameraDevice); if (camera_status != ACAMERA_OK) { LOGI("Failed to open camera device (id: %s)\n", selectedCameraId); } camera_status = ACameraDevice_createCaptureRequest(cameraDevice, TEMPLATE_PREVIEW, &captureRequest); if (camera_status != ACAMERA_OK) { LOGI("Failed to create preview capture request (id: %s)\n", selectedCameraId); } ACaptureSessionOutputContainer_create(&captureSessionOutputContainer); captureSessionStateCallbacks.onReady = capture_session_on_ready; captureSessionStateCallbacks.onActive = capture_session_on_active; captureSessionStateCallbacks.onClosed = capture_session_on_closed; ACameraMetadata_free(cameraMetadata); ACameraManager_deleteCameraIdList(cameraIdList); ACameraManager_delete(cameraManager); media_status_t status; // status = AImageReader_new(1920, 1080, AIMAGE_FORMAT_YUV_420_888, 5, &mAImageReader); status = AImageReader_new(1920, 1080, AIMAGE_FORMAT_JPEG, 5, &mAImageReader); if (status != AMEDIA_OK) { LOGI("AImageReader_new error\n"); return false; } AImageReader_ImageListener listener{ .context = this, .onImageAvailable = OnImageCallback, }; AImageReader_setImageListener(mAImageReader, &listener); //ANativeWindow *mNativeWindow; status = AImageReader_getWindow(mAImageReader, &theNativeWindow); if (status != AMEDIA_OK) { LOGI("AImageReader_getWindow error\n"); return false; } LOGI("Surface is prepared in %p.\n", theNativeWindow); ACameraOutputTarget_create(theNativeWindow, &cameraOutputTarget); ACaptureRequest_addTarget(captureRequest, cameraOutputTarget); ACaptureSessionOutput_create(theNativeWindow, &sessionOutput); ACaptureSessionOutputContainer_add(captureSessionOutputContainer, sessionOutput); ACameraDevice_createCaptureSession(cameraDevice, captureSessionOutputContainer, &captureSessionStateCallbacks, &captureSession); // ACameraCaptureSession_setRepeatingRequest(captureSession, NULL, 1, &captureRequest, NULL); ACameraCaptureSession_capture(captureSession, NULL, 1, &captureRequest, NULL); LOGI("Surface is prepared in here.\n"); return true; } ACameraCaptureSession_stateCallbacks* CPhoneDevice::GetSessionListener() { static ACameraCaptureSession_stateCallbacks sessionListener = { .context = this, .onClosed = CPhoneDevice::capture_session_on_closed, .onReady = CPhoneDevice::capture_session_on_ready, .onActive = CPhoneDevice::capture_session_on_active, }; return &sessionListener; } void CPhoneDevice::ImageCallback(AImageReader *reader) { int32_t format; AImage *image = nullptr; media_status_t status = AImageReader_acquireNextImage(reader, &image); LOGI("ImageCallback\n"); if (status == AMEDIA_OK && image) { LOGI("ImageCallback\n"); AImage_delete(image); } } void CPhoneDevice::OnImageCallback(void *ctx, AImageReader *reader) { CPhoneDevice* pThis = reinterpret_cast(ctx); AImage *image = nullptr; media_status_t status = AImageReader_acquireNextImage(reader, &image); if (status == AMEDIA_OK && image) { WriteFile(pThis, image); AImage_delete(image); // delete pThis; } } bool CPhoneDevice::WriteFile(CPhoneDevice* pThis, AImage *image) { int planeCount = 0; media_status_t status = AImage_getNumberOfPlanes(image, &planeCount); LOGI("Info: getNumberOfPlanes() planeCount = %d", planeCount); if (!(status == AMEDIA_OK && planeCount == 1)) { LOGE("Error: getNumberOfPlanes() planeCount = %d", planeCount); return false; } uint8_t *data = nullptr; int len = 0; AImage_getPlaneData(image, 0, &data, &len); std::string path = pThis->GetFileName(); bool res = false; FILE *file = fopen(path.c_str(), "wb"); if (file && data && len) { fwrite(data, 1, len, file); fclose(file); LOGE("Capture: %s", path.c_str()); res = true; } else { if (file) fclose(file); } return res; } std::string CPhoneDevice::GetFileName() const { return mPath; } void CPhoneDevice::camera_device_on_disconnected(void *context, ACameraDevice *device) { LOGI("Camera(id: %s) is diconnected.\n", ACameraDevice_getId(device)); CPhoneDevice* pThis = (CPhoneDevice*)context; delete pThis; } void CPhoneDevice::camera_device_on_error(void *context, ACameraDevice *device, int error) { LOGI("Error(code: %d) on Camera(id: %s).\n", error, ACameraDevice_getId(device)); } void CPhoneDevice::capture_session_on_ready(void *context, ACameraCaptureSession *session) { LOGI("Session is ready. %p\n", session); } void CPhoneDevice::capture_session_on_active(void *context, ACameraCaptureSession *session) { LOGI("Session is activated. %p\n", session); } void CPhoneDevice::capture_session_on_closed(void *context, ACameraCaptureSession *session) { LOGI("Session is closed. %p\n", session); }