From 5d8780d31e0d165cb506c286ab31da634a47dcb6 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 26 Feb 2022 11:43:33 +0800 Subject: [PATCH] Adjust file path Signed-off-by: unknown --- frameworks/BUILD.gn | 9 +- .../include}/camera_device_client.h | 120 +- .../include}/camera_service_client.h | 122 +- .../{ => binder/src}/camera_device_client.cpp | 660 +++---- .../src}/camera_service_client.cpp | 628 +++--- services/BUILD.gn | 15 +- services/{ => impl}/include/camera_device.h | 250 +-- services/{ => impl}/include/camera_service.h | 98 +- services/{ => impl}/src/camera_device.cpp | 1706 ++++++++--------- services/{ => impl}/src/camera_service.cpp | 372 ++-- services/{ => server}/include/camera_server.h | 116 +- services/{ => server}/include/camera_type.h | 112 +- services/{ => server}/src/camera_server.cpp | 734 +++---- services/{ => server}/src/samgr_camera.cpp | 186 +- 14 files changed, 2568 insertions(+), 2560 deletions(-) rename frameworks/{ => binder/include}/camera_device_client.h (96%) mode change 100755 => 100644 rename frameworks/{ => binder/include}/camera_service_client.h (97%) mode change 100755 => 100644 rename frameworks/{ => binder/src}/camera_device_client.cpp (96%) mode change 100755 => 100644 rename frameworks/{ => binder/src}/camera_service_client.cpp (97%) mode change 100755 => 100644 rename services/{ => impl}/include/camera_device.h (96%) mode change 100755 => 100644 rename services/{ => impl}/include/camera_service.h (97%) mode change 100755 => 100644 rename services/{ => impl}/src/camera_device.cpp (96%) mode change 100755 => 100644 rename services/{ => impl}/src/camera_service.cpp (97%) mode change 100755 => 100644 rename services/{ => server}/include/camera_server.h (97%) mode change 100755 => 100644 rename services/{ => server}/include/camera_type.h (96%) mode change 100755 => 100644 rename services/{ => server}/src/camera_server.cpp (97%) mode change 100755 => 100644 rename services/{ => server}/src/samgr_camera.cpp (96%) mode change 100755 => 100644 diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index a8593f9..d49fbd3 100755 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -12,20 +12,23 @@ import("//build/lite/config/component/lite_component.gni") shared_library("camera_lite") { sources = [ + "binder/src/camera_device_client.cpp", + "binder/src/camera_service_client.cpp", "camera_ability.cpp", "camera_client.cpp", "camera_config.cpp", - "camera_device_client.cpp", "camera_impl.cpp", "camera_info_impl.cpp", "camera_kit.cpp", "camera_manager.cpp", - "camera_service_client.cpp", "event_handler.cpp", "frame_config.cpp", ] include_dirs = [ - "//foundation/multimedia/camera_lite/services/include", + "//foundation/multimedia/camera_lite/services/server/include", + "//foundation/multimedia/camera_lite/services/impl/include", + "//foundation/multimedia/camera_lite/frameworks/binder/include", + "//foundation/multimedia/camera_lite/frameworks", "//utils/native/lite/include", "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", diff --git a/frameworks/camera_device_client.h b/frameworks/binder/include/camera_device_client.h old mode 100755 new mode 100644 similarity index 96% rename from frameworks/camera_device_client.h rename to frameworks/binder/include/camera_device_client.h index f855f9f..3229b54 --- a/frameworks/camera_device_client.h +++ b/frameworks/binder/include/camera_device_client.h @@ -1,61 +1,61 @@ -/* - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. - * 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. - */ - -#ifndef CAMERA_DEVICE_CLIENT_H -#define CAMERA_DEVICE_CLIENT_H - -#include "camera_config.h" -#include "camera_client.h" -#include "iproxy_client.h" -#include "media_log.h" -#include "liteipc_adapter.h" -#include "camera_type.h" - -#include - -namespace OHOS { -namespace Media { -class CameraImpl; -class CameraDeviceClient { -public: - static CameraDeviceClient *GetInstance(); - static int Callback(void* owner, int code, IpcIo *reply); - - ~CameraDeviceClient(); - - int32_t SetCameraConfig(CameraConfig &cc); - int32_t TriggerLoopingCapture(FrameConfig &fc); - void StopLoopingCapture(); - int32_t TriggerSingleCapture(FrameConfig &fc); - void Release(); - - void SetCameraId(std::string &cameraId); - void SetCameraImpl(CameraImpl *cameraImpl); - void SetCameraCallback(); - static int32_t SurfaceRequestHandler(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg); - static int32_t DeviceClientCallback(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg); -private: - CameraDeviceClient(); - CameraClient *cameraClient_ = nullptr; - IClientProxy *proxy_ = nullptr; - std::string cameraId_; - CameraImpl *cameraImpl_ = nullptr; - CallBackPara *para_ = nullptr; - SvcIdentity sid_; - int32_t ret_ = 0; -}; -} -} +/* + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef CAMERA_DEVICE_CLIENT_H +#define CAMERA_DEVICE_CLIENT_H + +#include "camera_config.h" +#include "camera_client.h" +#include "iproxy_client.h" +#include "media_log.h" +#include "liteipc_adapter.h" +#include "camera_type.h" + +#include + +namespace OHOS { +namespace Media { +class CameraImpl; +class CameraDeviceClient { +public: + static CameraDeviceClient *GetInstance(); + static int Callback(void* owner, int code, IpcIo *reply); + + ~CameraDeviceClient(); + + int32_t SetCameraConfig(CameraConfig &cc); + int32_t TriggerLoopingCapture(FrameConfig &fc); + void StopLoopingCapture(); + int32_t TriggerSingleCapture(FrameConfig &fc); + void Release(); + + void SetCameraId(std::string &cameraId); + void SetCameraImpl(CameraImpl *cameraImpl); + void SetCameraCallback(); + static int32_t SurfaceRequestHandler(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg); + static int32_t DeviceClientCallback(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg); +private: + CameraDeviceClient(); + CameraClient *cameraClient_ = nullptr; + IClientProxy *proxy_ = nullptr; + std::string cameraId_; + CameraImpl *cameraImpl_ = nullptr; + CallBackPara *para_ = nullptr; + SvcIdentity sid_; + int32_t ret_ = 0; +}; +} +} #endif // CAMERA_DEVICE_CLIENT_H \ No newline at end of file diff --git a/frameworks/camera_service_client.h b/frameworks/binder/include/camera_service_client.h old mode 100755 new mode 100644 similarity index 97% rename from frameworks/camera_service_client.h rename to frameworks/binder/include/camera_service_client.h index d08bdd5..d1a29fa --- a/frameworks/camera_service_client.h +++ b/frameworks/binder/include/camera_service_client.h @@ -1,61 +1,61 @@ -/* - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. - * 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. - */ -#ifndef CAMERA_SERVICE_CLIENT_H -#define CAMERA_SERVICE_CLIENT_H - -#include "liteipc_adapter.h" -#include "serializer.h" -#include "camera_ability.h" -#include "camera_info_impl.h" -#include "iproxy_client.h" -#include "camera_service_callback.h" -#include "camera_client.h" -#include "camera_type.h" - -namespace OHOS { -namespace Media { -class CameraServiceClient { -public: - ~CameraServiceClient(); - static CameraServiceClient *GetInstance(); - static int Callback(void* owner, int code, IpcIo *reply); - void InitCameraServiceClient(CameraServiceCallback *callback); - std::list GetCameraIdList(); - CameraAbility *GetCameraAbility(std::string &cameraId); - CameraInfo *GetCameraInfo(std::string &cameraId); - void CreateCamera(std::string cameraId); - static int32_t ServiceClientCallback(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg); - uint8_t GetCameraModeNum(); - int32_t SetCameraMode(uint8_t modeIndex); - -private: - CameraServiceClient(); - IClientProxy* proxy_ = nullptr; - CameraClient *cameraClient_ = nullptr; - std::list list_; - std::string cameraIdForAbility; - std::map deviceAbilityMap_; - std::string cameraIdForInfo; - std::map deviceInfoMap_; - CameraServiceCallback *cameraServiceCb_ = nullptr; - SvcIdentity sid_; - CallBackPara *para_ = nullptr; - uint8_t cameraModeNum = 0; - int32_t ret_ = 0; -}; -} // namespace Media -} // namespace OHOS - -#endif // CAMERA_SERVICE_CLIENT_H +/* + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. + * 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. + */ +#ifndef CAMERA_SERVICE_CLIENT_H +#define CAMERA_SERVICE_CLIENT_H + +#include "liteipc_adapter.h" +#include "serializer.h" +#include "camera_ability.h" +#include "camera_info_impl.h" +#include "iproxy_client.h" +#include "camera_service_callback.h" +#include "camera_client.h" +#include "camera_type.h" + +namespace OHOS { +namespace Media { +class CameraServiceClient { +public: + ~CameraServiceClient(); + static CameraServiceClient *GetInstance(); + static int Callback(void* owner, int code, IpcIo *reply); + void InitCameraServiceClient(CameraServiceCallback *callback); + std::list GetCameraIdList(); + CameraAbility *GetCameraAbility(std::string &cameraId); + CameraInfo *GetCameraInfo(std::string &cameraId); + void CreateCamera(std::string cameraId); + static int32_t ServiceClientCallback(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg); + uint8_t GetCameraModeNum(); + int32_t SetCameraMode(uint8_t modeIndex); + +private: + CameraServiceClient(); + IClientProxy* proxy_ = nullptr; + CameraClient *cameraClient_ = nullptr; + std::list list_; + std::string cameraIdForAbility; + std::map deviceAbilityMap_; + std::string cameraIdForInfo; + std::map deviceInfoMap_; + CameraServiceCallback *cameraServiceCb_ = nullptr; + SvcIdentity sid_; + CallBackPara *para_ = nullptr; + uint8_t cameraModeNum = 0; + int32_t ret_ = 0; +}; +} // namespace Media +} // namespace OHOS + +#endif // CAMERA_SERVICE_CLIENT_H diff --git a/frameworks/camera_device_client.cpp b/frameworks/binder/src/camera_device_client.cpp old mode 100755 new mode 100644 similarity index 96% rename from frameworks/camera_device_client.cpp rename to frameworks/binder/src/camera_device_client.cpp index cb8d6d7..7a9401d --- a/frameworks/camera_device_client.cpp +++ b/frameworks/binder/src/camera_device_client.cpp @@ -1,331 +1,331 @@ -/* - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. - * 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. - */ - -#include "camera_device_client.h" -#include "liteipc_adapter.h" -#include "serializer.h" -#include "camera_type.h" -#include "media_log.h" -#include "camera_config.h" -#include "frame_config.h" -#include "surface.h" -#include "surface_impl.h" -#include "camera_impl.h" - -#include - -using namespace std; -namespace OHOS { -namespace Media { -CameraDeviceClient *CameraDeviceClient::GetInstance() -{ - static CameraDeviceClient client; - return &client; -} - -CameraDeviceClient::CameraDeviceClient() -{ - cameraClient_ = CameraClient::GetInstance(); - if (cameraClient_->InitCameraClient()) { - proxy_ = cameraClient_->GetIClientProxy(); - } -} - -CameraDeviceClient::~CameraDeviceClient() -{ - if (para_ != nullptr) { - delete para_; - para_ = nullptr; - } - UnregisterIpcCallback(sid_); -} - -void CameraDeviceClient::SetCameraId(string &cameraId) -{ - cameraId_ = cameraId; -} - -void CameraDeviceClient::SetCameraImpl(CameraImpl *cameraImpl) -{ - cameraImpl_ = cameraImpl; -} - -int CameraDeviceClient::Callback(void* owner, int code, IpcIo *reply) -{ - if (code != 0) { - MEDIA_ERR_LOG("Callback error. (code=%d)", code); - return -1; - } - if (owner == nullptr) { - return -1; - } - CallBackPara* para = (CallBackPara*)owner; - switch (para->funcId) { - case CAMERA_SERVER_SET_CAMERA_CALLBACK: - MEDIA_INFO_LOG("Camera server set callback success."); - break; - default: - break; - } - return 0; -} - -int32_t CameraDeviceClient::SetCameraConfig(CameraConfig &cc) -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1); - if (cameraId_.empty()) { - MEDIA_ERR_LOG("no camera exist."); - return MEDIA_ERR; - } - IpcIoPushString(&io, cameraId_.c_str()); - para_->data = this; - para_->cameraConfig = &cc; - CallBackPara para = {}; - para.funcId = CAEMRA_SERVER_SET_CAMERA_CONFIG; - para.data = this; - uint32_t ret = proxy_->Invoke(proxy_, CAEMRA_SERVER_SET_CAMERA_CONFIG, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Set camera config ipc transmission failed. (ret=%d)", ret); - return MEDIA_ERR; - } - return MEDIA_OK; -} - -int32_t SerilizeFrameConfig(IpcIo &io, FrameConfig &fc, uint32_t maxSurfaceNum) -{ - IpcIoPushInt32(&io, fc.GetFrameConfigType()); - list surfaceList = fc.GetSurfaces(); - if (maxSurfaceNum < surfaceList.size()) { - MEDIA_ERR_LOG("Too many surfaces. (maxSurfaceNum=%u, sufaceNum=%d)", maxSurfaceNum, surfaceList.size()); - return MEDIA_ERR; - } - IpcIoPushUint32(&io, surfaceList.size()); - for (auto &surface : surfaceList) { - dynamic_cast(surface)->WriteIoIpcIo(io); - MEDIA_DEBUG_LOG("Add surface"); - } - int32_t qfactor = -1; - fc.GetParameter(PARAM_KEY_IMAGE_ENCODE_QFACTOR, qfactor); - IpcIoPushInt32(&io, qfactor); - - int32_t streamFps = 0; - fc.GetParameter(CAM_FRAME_FPS, streamFps); - IpcIoPushInt32(&io, streamFps); - - int32_t invertMode = 0; - fc.GetParameter(CAM_IMAGE_INVERT_MODE, invertMode); - IpcIoPushInt32(&io, invertMode); - - CameraRect streamCrop; - fc.GetParameter(CAM_IMAGE_CROP_RECT, streamCrop); - IpcIoPushInt32(&io, streamCrop.x); - IpcIoPushInt32(&io, streamCrop.y); - IpcIoPushInt32(&io, streamCrop.w); - IpcIoPushInt32(&io, streamCrop.h); - - int32_t format = -1; - fc.GetParameter(CAM_IMAGE_FORMAT, format); - IpcIoPushInt32(&io, format); - if (fc.GetFrameConfigType() != FRAME_CONFIG_RECORD) { - uint8_t data[PRIVATE_TAG_LEN]; - fc.GetVendorParameter(data, sizeof(data)); - BuffPtr dataBuff = { - .buffSz = sizeof(data), - .buff = (void *)data - }; - IpcIoPushDataBuff(&io, &dataBuff); - } - - return MEDIA_OK; -} - - -int32_t CameraDeviceClient::TriggerLoopingCapture(FrameConfig &fc) -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - constexpr uint32_t maxSurfaceNum = 2; // 2 surfaces at most - /* serilize parameters */ - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, maxSurfaceNum); - if (cameraId_.empty()) { - MEDIA_ERR_LOG("no camera exist."); - return MEDIA_ERR; - } - IpcIoPushString(&io, cameraId_.c_str()); - if (SerilizeFrameConfig(io, fc, maxSurfaceNum) != MEDIA_OK) { - MEDIA_ERR_LOG("Serilize the frameconfig failed."); - return MEDIA_ERR; - } - para_->data = this; - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_TRIGGER_LOOPING_CAPTURE; - para.data = this; - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_TRIGGER_LOOPING_CAPTURE, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Trigger looping capture ipc transmission failed. (ret=%d)", ret); - return MEDIA_ERR; - } - return ret_; -} - -int32_t CameraDeviceClient::TriggerSingleCapture(FrameConfig &fc) -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - constexpr uint32_t maxSurfaceNum = 2; // 2 surfaces at most - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, maxSurfaceNum); - - if (cameraId_.empty()) { - MEDIA_ERR_LOG("no camera exist."); - return MEDIA_ERR; - } - IpcIoPushString(&io, cameraId_.c_str()); - if (SerilizeFrameConfig(io, fc, maxSurfaceNum) != MEDIA_OK) { - MEDIA_ERR_LOG("Serilize the frameconfig failed."); - return MEDIA_ERR; - } - para_->frameConfig = &fc; - para_->data = this; - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_TRIGGER_SINGLE_CAPTURE; - para.data = this; - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_TRIGGER_SINGLE_CAPTURE, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Trigger single capture ipc transmission failed. (ret=%d)", ret); - return MEDIA_ERR; - } - return ret_; -} - -void CameraDeviceClient::StopLoopingCapture() -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - if (cameraId_.empty()) { - MEDIA_ERR_LOG("no camera exist."); - return; - } - IpcIoPushString(&io, cameraId_.c_str()); - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_STOP_LOOPING_CAPTURE; - para.data = this; - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_STOP_LOOPING_CAPTURE, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Stop Looping capture ipc transmission failed. (ret=%d)", ret); - } -} - -void CameraDeviceClient::Release() -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1); - if (cameraId_.empty()) { - MEDIA_ERR_LOG("no camera exist."); - return; - } - if (proxy_ == nullptr) { - return; - } - IpcIoPushString(&io, cameraId_.c_str()); - IpcIoPushSvc(&io, &sid_); - para_->data = this; - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_CLOSE_CAMERA; - para.data = this; - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_CLOSE_CAMERA, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Stop Looping capture ipc transmission failed. (ret=%d)", ret); - } -} - -void CameraDeviceClient::SetCameraCallback() -{ - para_ = new (nothrow) CallBackPara; - if (para_ == nullptr) { - MEDIA_ERR_LOG("para_ is null."); - return; - } - int32_t ret = RegisterIpcCallback(DeviceClientCallback, 0, IPC_WAIT_FOREVER, &sid_, para_); - if (ret != LITEIPC_OK) { - MEDIA_ERR_LOG("RegisteIpcCallback failed\n"); - return; - } - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1); - IpcIoPushSvc(&io, &sid_); - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_SET_CAMERA_CALLBACK; - uint32_t ans = proxy_->Invoke(proxy_, CAMERA_SERVER_SET_CAMERA_CALLBACK, &io, ¶, Callback); - if (ans != 0) { - MEDIA_ERR_LOG("Stop Looping capture ipc transmission failed. (ret=%d)", ans); - } -} - -int32_t CameraDeviceClient::DeviceClientCallback(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg) -{ - if (ipcMsg == nullptr) { - MEDIA_ERR_LOG("call back error, ipcMsg is null\n"); - return MEDIA_ERR; - } - if (arg == nullptr) { - MEDIA_ERR_LOG("call back error, arg is null\n"); - return MEDIA_ERR; - } - CallBackPara *para = static_cast(arg); - CameraDeviceClient *client = static_cast(para->data); - uint32_t funcId; - (void)GetCode(ipcMsg, &funcId); - MEDIA_INFO_LOG("DeviceCallback, funcId=%d", funcId); - switch (funcId) { - case ON_CAMERA_CONFIGURED: { - int32_t ret = IpcIoPopInt32(io); - CameraConfig *cc = static_cast(para->cameraConfig); - client->cameraImpl_->OnConfigured(ret, *cc); - break; - } - case ON_TRIGGER_SINGLE_CAPTURE_FINISHED: { - int32_t ret = IpcIoPopInt32(io); - FrameConfig *fc = static_cast(para->frameConfig); - client->cameraImpl_->OnFrameFinished(ret, *fc); - client->ret_ = ret; - break; - } - case ON_TRIGGER_LOOPING_CAPTURE_FINISHED: { - int32_t ret = IpcIoPopInt32(io); - int32_t streamId = IpcIoPopInt32(io); - MEDIA_INFO_LOG("ON_TRIGGER_LOOPING_CAPTURE_FINISHED : (ret=%d, streamId=%d).", ret, streamId); - client->ret_ = ret; - break; - } - case ON_CAMERA_STATUS_CHANGE: { - int32_t ret = IpcIoPopInt32(io); - MEDIA_INFO_LOG("ON_CAMERA_STATUS_CHANGE : ret=%d.", ret); - break; - } - default: { - MEDIA_ERR_LOG("unsupport funId\n"); - break; - } - } - client->cameraClient_->ClearIpcMsg(ipcMsg); - return MEDIA_OK; -} -} +/* + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. + * 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. + */ + +#include "camera_device_client.h" +#include "liteipc_adapter.h" +#include "serializer.h" +#include "camera_type.h" +#include "media_log.h" +#include "camera_config.h" +#include "frame_config.h" +#include "surface.h" +#include "surface_impl.h" +#include "camera_impl.h" + +#include + +using namespace std; +namespace OHOS { +namespace Media { +CameraDeviceClient *CameraDeviceClient::GetInstance() +{ + static CameraDeviceClient client; + return &client; +} + +CameraDeviceClient::CameraDeviceClient() +{ + cameraClient_ = CameraClient::GetInstance(); + if (cameraClient_->InitCameraClient()) { + proxy_ = cameraClient_->GetIClientProxy(); + } +} + +CameraDeviceClient::~CameraDeviceClient() +{ + if (para_ != nullptr) { + delete para_; + para_ = nullptr; + } + UnregisterIpcCallback(sid_); +} + +void CameraDeviceClient::SetCameraId(string &cameraId) +{ + cameraId_ = cameraId; +} + +void CameraDeviceClient::SetCameraImpl(CameraImpl *cameraImpl) +{ + cameraImpl_ = cameraImpl; +} + +int CameraDeviceClient::Callback(void* owner, int code, IpcIo *reply) +{ + if (code != 0) { + MEDIA_ERR_LOG("Callback error. (code=%d)", code); + return -1; + } + if (owner == nullptr) { + return -1; + } + CallBackPara* para = (CallBackPara*)owner; + switch (para->funcId) { + case CAMERA_SERVER_SET_CAMERA_CALLBACK: + MEDIA_INFO_LOG("Camera server set callback success."); + break; + default: + break; + } + return 0; +} + +int32_t CameraDeviceClient::SetCameraConfig(CameraConfig &cc) +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1); + if (cameraId_.empty()) { + MEDIA_ERR_LOG("no camera exist."); + return MEDIA_ERR; + } + IpcIoPushString(&io, cameraId_.c_str()); + para_->data = this; + para_->cameraConfig = &cc; + CallBackPara para = {}; + para.funcId = CAEMRA_SERVER_SET_CAMERA_CONFIG; + para.data = this; + uint32_t ret = proxy_->Invoke(proxy_, CAEMRA_SERVER_SET_CAMERA_CONFIG, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Set camera config ipc transmission failed. (ret=%d)", ret); + return MEDIA_ERR; + } + return MEDIA_OK; +} + +int32_t SerilizeFrameConfig(IpcIo &io, FrameConfig &fc, uint32_t maxSurfaceNum) +{ + IpcIoPushInt32(&io, fc.GetFrameConfigType()); + list surfaceList = fc.GetSurfaces(); + if (maxSurfaceNum < surfaceList.size()) { + MEDIA_ERR_LOG("Too many surfaces. (maxSurfaceNum=%u, sufaceNum=%d)", maxSurfaceNum, surfaceList.size()); + return MEDIA_ERR; + } + IpcIoPushUint32(&io, surfaceList.size()); + for (auto &surface : surfaceList) { + dynamic_cast(surface)->WriteIoIpcIo(io); + MEDIA_DEBUG_LOG("Add surface"); + } + int32_t qfactor = -1; + fc.GetParameter(PARAM_KEY_IMAGE_ENCODE_QFACTOR, qfactor); + IpcIoPushInt32(&io, qfactor); + + int32_t streamFps = 0; + fc.GetParameter(CAM_FRAME_FPS, streamFps); + IpcIoPushInt32(&io, streamFps); + + int32_t invertMode = 0; + fc.GetParameter(CAM_IMAGE_INVERT_MODE, invertMode); + IpcIoPushInt32(&io, invertMode); + + CameraRect streamCrop; + fc.GetParameter(CAM_IMAGE_CROP_RECT, streamCrop); + IpcIoPushInt32(&io, streamCrop.x); + IpcIoPushInt32(&io, streamCrop.y); + IpcIoPushInt32(&io, streamCrop.w); + IpcIoPushInt32(&io, streamCrop.h); + + int32_t format = -1; + fc.GetParameter(CAM_IMAGE_FORMAT, format); + IpcIoPushInt32(&io, format); + if (fc.GetFrameConfigType() != FRAME_CONFIG_RECORD) { + uint8_t data[PRIVATE_TAG_LEN]; + fc.GetVendorParameter(data, sizeof(data)); + BuffPtr dataBuff = { + .buffSz = sizeof(data), + .buff = (void *)data + }; + IpcIoPushDataBuff(&io, &dataBuff); + } + + return MEDIA_OK; +} + + +int32_t CameraDeviceClient::TriggerLoopingCapture(FrameConfig &fc) +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + constexpr uint32_t maxSurfaceNum = 2; // 2 surfaces at most + /* serilize parameters */ + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, maxSurfaceNum); + if (cameraId_.empty()) { + MEDIA_ERR_LOG("no camera exist."); + return MEDIA_ERR; + } + IpcIoPushString(&io, cameraId_.c_str()); + if (SerilizeFrameConfig(io, fc, maxSurfaceNum) != MEDIA_OK) { + MEDIA_ERR_LOG("Serilize the frameconfig failed."); + return MEDIA_ERR; + } + para_->data = this; + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_TRIGGER_LOOPING_CAPTURE; + para.data = this; + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_TRIGGER_LOOPING_CAPTURE, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Trigger looping capture ipc transmission failed. (ret=%d)", ret); + return MEDIA_ERR; + } + return ret_; +} + +int32_t CameraDeviceClient::TriggerSingleCapture(FrameConfig &fc) +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + constexpr uint32_t maxSurfaceNum = 2; // 2 surfaces at most + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, maxSurfaceNum); + + if (cameraId_.empty()) { + MEDIA_ERR_LOG("no camera exist."); + return MEDIA_ERR; + } + IpcIoPushString(&io, cameraId_.c_str()); + if (SerilizeFrameConfig(io, fc, maxSurfaceNum) != MEDIA_OK) { + MEDIA_ERR_LOG("Serilize the frameconfig failed."); + return MEDIA_ERR; + } + para_->frameConfig = &fc; + para_->data = this; + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_TRIGGER_SINGLE_CAPTURE; + para.data = this; + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_TRIGGER_SINGLE_CAPTURE, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Trigger single capture ipc transmission failed. (ret=%d)", ret); + return MEDIA_ERR; + } + return ret_; +} + +void CameraDeviceClient::StopLoopingCapture() +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + if (cameraId_.empty()) { + MEDIA_ERR_LOG("no camera exist."); + return; + } + IpcIoPushString(&io, cameraId_.c_str()); + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_STOP_LOOPING_CAPTURE; + para.data = this; + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_STOP_LOOPING_CAPTURE, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Stop Looping capture ipc transmission failed. (ret=%d)", ret); + } +} + +void CameraDeviceClient::Release() +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1); + if (cameraId_.empty()) { + MEDIA_ERR_LOG("no camera exist."); + return; + } + if (proxy_ == nullptr) { + return; + } + IpcIoPushString(&io, cameraId_.c_str()); + IpcIoPushSvc(&io, &sid_); + para_->data = this; + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_CLOSE_CAMERA; + para.data = this; + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_CLOSE_CAMERA, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Stop Looping capture ipc transmission failed. (ret=%d)", ret); + } +} + +void CameraDeviceClient::SetCameraCallback() +{ + para_ = new (nothrow) CallBackPara; + if (para_ == nullptr) { + MEDIA_ERR_LOG("para_ is null."); + return; + } + int32_t ret = RegisterIpcCallback(DeviceClientCallback, 0, IPC_WAIT_FOREVER, &sid_, para_); + if (ret != LITEIPC_OK) { + MEDIA_ERR_LOG("RegisteIpcCallback failed\n"); + return; + } + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1); + IpcIoPushSvc(&io, &sid_); + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_SET_CAMERA_CALLBACK; + uint32_t ans = proxy_->Invoke(proxy_, CAMERA_SERVER_SET_CAMERA_CALLBACK, &io, ¶, Callback); + if (ans != 0) { + MEDIA_ERR_LOG("Stop Looping capture ipc transmission failed. (ret=%d)", ans); + } +} + +int32_t CameraDeviceClient::DeviceClientCallback(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg) +{ + if (ipcMsg == nullptr) { + MEDIA_ERR_LOG("call back error, ipcMsg is null\n"); + return MEDIA_ERR; + } + if (arg == nullptr) { + MEDIA_ERR_LOG("call back error, arg is null\n"); + return MEDIA_ERR; + } + CallBackPara *para = static_cast(arg); + CameraDeviceClient *client = static_cast(para->data); + uint32_t funcId; + (void)GetCode(ipcMsg, &funcId); + MEDIA_INFO_LOG("DeviceCallback, funcId=%d", funcId); + switch (funcId) { + case ON_CAMERA_CONFIGURED: { + int32_t ret = IpcIoPopInt32(io); + CameraConfig *cc = static_cast(para->cameraConfig); + client->cameraImpl_->OnConfigured(ret, *cc); + break; + } + case ON_TRIGGER_SINGLE_CAPTURE_FINISHED: { + int32_t ret = IpcIoPopInt32(io); + FrameConfig *fc = static_cast(para->frameConfig); + client->cameraImpl_->OnFrameFinished(ret, *fc); + client->ret_ = ret; + break; + } + case ON_TRIGGER_LOOPING_CAPTURE_FINISHED: { + int32_t ret = IpcIoPopInt32(io); + int32_t streamId = IpcIoPopInt32(io); + MEDIA_INFO_LOG("ON_TRIGGER_LOOPING_CAPTURE_FINISHED : (ret=%d, streamId=%d).", ret, streamId); + client->ret_ = ret; + break; + } + case ON_CAMERA_STATUS_CHANGE: { + int32_t ret = IpcIoPopInt32(io); + MEDIA_INFO_LOG("ON_CAMERA_STATUS_CHANGE : ret=%d.", ret); + break; + } + default: { + MEDIA_ERR_LOG("unsupport funId\n"); + break; + } + } + client->cameraClient_->ClearIpcMsg(ipcMsg); + return MEDIA_OK; +} +} } \ No newline at end of file diff --git a/frameworks/camera_service_client.cpp b/frameworks/binder/src/camera_service_client.cpp old mode 100755 new mode 100644 similarity index 97% rename from frameworks/camera_service_client.cpp rename to frameworks/binder/src/camera_service_client.cpp index fbe253b..2805439 --- a/frameworks/camera_service_client.cpp +++ b/frameworks/binder/src/camera_service_client.cpp @@ -1,315 +1,315 @@ -/* - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. - * 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. - */ -#include "camera_service_client.h" -#include "media_log.h" -#include "samgr_lite.h" -#include "camera_type.h" -#include "camera_manager.h" -#include "meta_data.h" -#include "camera_client.h" - -#include -#include - -using namespace std; -namespace OHOS { -namespace Media { -CameraServiceClient *CameraServiceClient::GetInstance() -{ - static CameraServiceClient client; - return &client; -} - -CameraServiceClient::CameraServiceClient() -{ - cameraClient_ = CameraClient::GetInstance(); -} - -CameraServiceClient::~CameraServiceClient() -{ - if (para_ != nullptr) { - delete para_; - para_ = nullptr; - } - UnregisterIpcCallback(sid_); -} - -void CameraServiceClient::InitCameraServiceClient(CameraServiceCallback *callback) -{ - cameraServiceCb_ = callback; - if (cameraClient_->InitCameraClient()) { - MEDIA_INFO_LOG("Camera client initialize success."); - proxy_ = cameraClient_->GetIClientProxy(); - list cameraList = CameraServiceClient::GetInstance()->GetCameraIdList(); - cameraServiceCb_->OnCameraServiceInitialized(cameraList); - } -} - -int CameraServiceClient::Callback(void* owner, int code, IpcIo *reply) -{ - if (code != 0) { - MEDIA_ERR_LOG("Callback error. (code=%d)", code); - return -1; - } - if (owner == nullptr) { - return -1; - } - CallBackPara* para = (CallBackPara*)owner; - switch (para->funcId) { - case CAMERA_SERVER_GET_CAMERA_ABILITY: { - CameraServiceClient *client = static_cast(para->data); - IpcIoPopUint32(reply); // Pop supportProperities uint32_t - // Get supported resolution. - uint32_t listSize = IpcIoPopUint32(reply); - uint32_t size; - list supportSizeList; - for (uint32_t i = 0; i < listSize; i++) { - CameraPicSize *cameraPicSize = static_cast(IpcIoPopFlatObj(reply, &size)); - if (cameraPicSize != nullptr) { - supportSizeList.emplace_back(*cameraPicSize); - } - } - // Get supported AfModes. - uint32_t afListSize = IpcIoPopUint32(reply); - list afModeList; - for (uint32_t i = 0; i < afListSize; i++) { - afModeList.emplace_back(IpcIoPopInt32(reply)); - } - // Get supported AeModes. - uint32_t aeListSize = IpcIoPopUint32(reply); - list aeModeList; - for (uint32_t i = 0; i < aeListSize; i++) { - aeModeList.emplace_back(IpcIoPopInt32(reply)); - } - - CameraAbility *cameraAbility = new (nothrow) CameraAbility; - if (cameraAbility != nullptr) { - cameraAbility->SetParameterRange(CAM_IMAGE_YUV420, supportSizeList); - cameraAbility->SetParameterRange(CAM_FORMAT_JPEG, supportSizeList); - cameraAbility->SetParameterRange(CAM_FORMAT_H264, supportSizeList); - cameraAbility->SetParameterRange(CAM_FORMAT_H265, supportSizeList); - cameraAbility->SetParameterRange(CAM_AF_MODE, afModeList); - cameraAbility->SetParameterRange(CAM_AE_MODE, aeModeList); - client->deviceAbilityMap_.insert( - pair(client->cameraIdForAbility, cameraAbility)); - } else { - MEDIA_ERR_LOG("Callback : cameraAbility construct failed."); - } - break; - } - case CAMERA_SERVER_GET_CAMERA_INFO: { - CameraServiceClient *client = static_cast(para->data); - int32_t cameraType = IpcIoPopInt32(reply); - int32_t cameraFacingType = IpcIoPopInt32(reply); - CameraInfo *cameraInfo = new (nothrow) CameraInfoImpl(cameraType, cameraFacingType); - if (cameraInfo != nullptr) { - client->deviceInfoMap_.insert(pair(client->cameraIdForInfo, cameraInfo)); - } else { - MEDIA_ERR_LOG("Callback : cameraAbility construct failed."); - } - break; - } - case CAMERA_SERVER_GET_CAMERAIDLIST: { - CameraServiceClient *client = static_cast(para->data); - uint32_t listSize = IpcIoPopUint32(reply); - for (uint32_t i = 0; i < listSize; i++) { - size_t sz; - string cameraId((const char*)(IpcIoPopString(reply, &sz))); - client->list_.emplace_back(cameraId); - MEDIA_INFO_LOG("Callback : cameraId %s", cameraId.c_str()); - } - break; - } - case CAMERA_SERVER_GET_CAMERA_MODE_NUM: { - CameraServiceClient *client = static_cast(para->data); - client->cameraModeNum = IpcIoPopUint8(reply); - break; - } - case CAMERA_SERVER_SET_CAMERA_MODE_NUM: { - CameraServiceClient *client = static_cast(para->data); - client->ret_ = IpcIoPopInt32(reply); - break; - } - default: - MEDIA_ERR_LOG("unsupport funcId."); - break; - } - return 0; -} - -list CameraServiceClient::GetCameraIdList() -{ - if (list_.empty()) { - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_GET_CAMERAIDLIST; - para.data = this; - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_GET_CAMERAIDLIST, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Get cameraId list ipc transmission failed. (ret=%d)", ret); - } - } - return list_; -} - -uint8_t CameraServiceClient::GetCameraModeNum() -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_GET_CAMERA_MODE_NUM; - para.data = this; - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_GET_CAMERA_MODE_NUM, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Get camera mode num failed. (ret=%d)", ret); - } - return this->cameraModeNum; -} - -CameraAbility *CameraServiceClient::GetCameraAbility(string &cameraId) -{ - std::map::iterator iter = deviceAbilityMap_.find(cameraId); - if (iter != deviceAbilityMap_.end()) { - return iter->second; - } - cameraIdForAbility = cameraId; - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - IpcIoPushString(&io, cameraId.c_str()); - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_GET_CAMERA_ABILITY; - para.data = this; - - // wait for callback. - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_GET_CAMERA_ABILITY, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Get camera ability ipc transmission failed. (ret=%d)", ret); - } - // find cameraAbility again. - iter = deviceAbilityMap_.find(cameraId); - if (iter != deviceAbilityMap_.end()) { - return iter->second; - } - MEDIA_ERR_LOG("Get cameraAbility of camera %s from cameraService failded", cameraId.c_str()); - return nullptr; -} - -CameraInfo *CameraServiceClient::GetCameraInfo(string &cameraId) -{ - std::map::iterator iter = deviceInfoMap_.find(cameraId); - if (iter != deviceInfoMap_.end()) { - return iter->second; - } - cameraIdForInfo = cameraId; - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - IpcIoPushString(&io, cameraId.c_str()); - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_GET_CAMERA_INFO; - para.data = this; - // wait for callback. - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_GET_CAMERA_INFO, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Get camera info ipc transmission failed. (ret=%d)", ret); - } - - iter = deviceInfoMap_.find(cameraId); - if (iter != deviceInfoMap_.end()) { - return iter->second; - } - MEDIA_ERR_LOG("Get cameraInfo of camera %s from cameraService failded", cameraId.c_str()); - return nullptr; -} - -int32_t CameraServiceClient::ServiceClientCallback(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg) -{ - if (ipcMsg == nullptr) { - MEDIA_ERR_LOG("call back error, ipcMsg is null\n"); - return MEDIA_ERR; - } - if (arg == nullptr) { - MEDIA_ERR_LOG("call back error, arg is null\n"); - return MEDIA_ERR; - } - CallBackPara* para = static_cast(arg); - CameraServiceClient *client = static_cast(para->data); - uint32_t funcId; - (void)GetCode(ipcMsg, &funcId); - MEDIA_INFO_LOG("ServiceClientCallback, funcId=%d", funcId); - switch (funcId) { - case ON_CAMERA_STATUS_CHANGE: { - CameraServiceCallback::CameraStauts cameraStatus = - static_cast(IpcIoPopInt32(io)); - string cameraId = para->cameraId; - client->cameraServiceCb_->OnCameraStatusChange(cameraId, cameraStatus); - break; - } - default: { - MEDIA_ERR_LOG("unsupport funId\n"); - break; - } - } - client->cameraClient_->ClearIpcMsg(ipcMsg); - return MEDIA_OK; -} - -int32_t CameraServiceClient::SetCameraMode(uint8_t modeIndex) -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - IpcIoPushUint8(&io, modeIndex); - CallBackPara para = {}; - para.funcId = CAMERA_SERVER_SET_CAMERA_MODE_NUM; - para.data = this; - uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_SET_CAMERA_MODE_NUM, &io, ¶, Callback); - if (ret != 0) { - MEDIA_ERR_LOG("Set camera mode failed.(ret=%d)", ret); - return ret; - } - return this->ret_; -} - -void CameraServiceClient::CreateCamera(string cameraId) -{ - para_ = new (nothrow) CallBackPara; - if (para_ == nullptr) { - MEDIA_ERR_LOG("para_ is null, failed."); - return; - } - para_->cameraId = cameraId; - para_->data = this; - int32_t ret = RegisterIpcCallback(ServiceClientCallback, 0, IPC_WAIT_FOREVER, &sid_, para_); - if (ret != LITEIPC_OK) { - MEDIA_ERR_LOG("RegisteIpcCallback failed, (ret=%d).", ret); - return; - } - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1); - IpcIoPushString(&io, cameraId.c_str()); - IpcIoPushSvc(&io, &sid_); - uint32_t ans = proxy_->Invoke(proxy_, CAMERA_SERVER_CREATE_CAMERA, &io, para_, Callback); - if (ans != 0) { - MEDIA_ERR_LOG("Create camera ipc transmission failed. (ret=%d)", ans); - } -} -} +/* + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. + * 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. + */ +#include "camera_service_client.h" +#include "media_log.h" +#include "samgr_lite.h" +#include "camera_type.h" +#include "camera_manager.h" +#include "meta_data.h" +#include "camera_client.h" + +#include +#include + +using namespace std; +namespace OHOS { +namespace Media { +CameraServiceClient *CameraServiceClient::GetInstance() +{ + static CameraServiceClient client; + return &client; +} + +CameraServiceClient::CameraServiceClient() +{ + cameraClient_ = CameraClient::GetInstance(); +} + +CameraServiceClient::~CameraServiceClient() +{ + if (para_ != nullptr) { + delete para_; + para_ = nullptr; + } + UnregisterIpcCallback(sid_); +} + +void CameraServiceClient::InitCameraServiceClient(CameraServiceCallback *callback) +{ + cameraServiceCb_ = callback; + if (cameraClient_->InitCameraClient()) { + MEDIA_INFO_LOG("Camera client initialize success."); + proxy_ = cameraClient_->GetIClientProxy(); + list cameraList = CameraServiceClient::GetInstance()->GetCameraIdList(); + cameraServiceCb_->OnCameraServiceInitialized(cameraList); + } +} + +int CameraServiceClient::Callback(void* owner, int code, IpcIo *reply) +{ + if (code != 0) { + MEDIA_ERR_LOG("Callback error. (code=%d)", code); + return -1; + } + if (owner == nullptr) { + return -1; + } + CallBackPara* para = (CallBackPara*)owner; + switch (para->funcId) { + case CAMERA_SERVER_GET_CAMERA_ABILITY: { + CameraServiceClient *client = static_cast(para->data); + IpcIoPopUint32(reply); // Pop supportProperities uint32_t + // Get supported resolution. + uint32_t listSize = IpcIoPopUint32(reply); + uint32_t size; + list supportSizeList; + for (uint32_t i = 0; i < listSize; i++) { + CameraPicSize *cameraPicSize = static_cast(IpcIoPopFlatObj(reply, &size)); + if (cameraPicSize != nullptr) { + supportSizeList.emplace_back(*cameraPicSize); + } + } + // Get supported AfModes. + uint32_t afListSize = IpcIoPopUint32(reply); + list afModeList; + for (uint32_t i = 0; i < afListSize; i++) { + afModeList.emplace_back(IpcIoPopInt32(reply)); + } + // Get supported AeModes. + uint32_t aeListSize = IpcIoPopUint32(reply); + list aeModeList; + for (uint32_t i = 0; i < aeListSize; i++) { + aeModeList.emplace_back(IpcIoPopInt32(reply)); + } + + CameraAbility *cameraAbility = new (nothrow) CameraAbility; + if (cameraAbility != nullptr) { + cameraAbility->SetParameterRange(CAM_IMAGE_YUV420, supportSizeList); + cameraAbility->SetParameterRange(CAM_FORMAT_JPEG, supportSizeList); + cameraAbility->SetParameterRange(CAM_FORMAT_H264, supportSizeList); + cameraAbility->SetParameterRange(CAM_FORMAT_H265, supportSizeList); + cameraAbility->SetParameterRange(CAM_AF_MODE, afModeList); + cameraAbility->SetParameterRange(CAM_AE_MODE, aeModeList); + client->deviceAbilityMap_.insert( + pair(client->cameraIdForAbility, cameraAbility)); + } else { + MEDIA_ERR_LOG("Callback : cameraAbility construct failed."); + } + break; + } + case CAMERA_SERVER_GET_CAMERA_INFO: { + CameraServiceClient *client = static_cast(para->data); + int32_t cameraType = IpcIoPopInt32(reply); + int32_t cameraFacingType = IpcIoPopInt32(reply); + CameraInfo *cameraInfo = new (nothrow) CameraInfoImpl(cameraType, cameraFacingType); + if (cameraInfo != nullptr) { + client->deviceInfoMap_.insert(pair(client->cameraIdForInfo, cameraInfo)); + } else { + MEDIA_ERR_LOG("Callback : cameraAbility construct failed."); + } + break; + } + case CAMERA_SERVER_GET_CAMERAIDLIST: { + CameraServiceClient *client = static_cast(para->data); + uint32_t listSize = IpcIoPopUint32(reply); + for (uint32_t i = 0; i < listSize; i++) { + size_t sz; + string cameraId((const char*)(IpcIoPopString(reply, &sz))); + client->list_.emplace_back(cameraId); + MEDIA_INFO_LOG("Callback : cameraId %s", cameraId.c_str()); + } + break; + } + case CAMERA_SERVER_GET_CAMERA_MODE_NUM: { + CameraServiceClient *client = static_cast(para->data); + client->cameraModeNum = IpcIoPopUint8(reply); + break; + } + case CAMERA_SERVER_SET_CAMERA_MODE_NUM: { + CameraServiceClient *client = static_cast(para->data); + client->ret_ = IpcIoPopInt32(reply); + break; + } + default: + MEDIA_ERR_LOG("unsupport funcId."); + break; + } + return 0; +} + +list CameraServiceClient::GetCameraIdList() +{ + if (list_.empty()) { + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_GET_CAMERAIDLIST; + para.data = this; + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_GET_CAMERAIDLIST, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Get cameraId list ipc transmission failed. (ret=%d)", ret); + } + } + return list_; +} + +uint8_t CameraServiceClient::GetCameraModeNum() +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_GET_CAMERA_MODE_NUM; + para.data = this; + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_GET_CAMERA_MODE_NUM, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Get camera mode num failed. (ret=%d)", ret); + } + return this->cameraModeNum; +} + +CameraAbility *CameraServiceClient::GetCameraAbility(string &cameraId) +{ + std::map::iterator iter = deviceAbilityMap_.find(cameraId); + if (iter != deviceAbilityMap_.end()) { + return iter->second; + } + cameraIdForAbility = cameraId; + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + IpcIoPushString(&io, cameraId.c_str()); + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_GET_CAMERA_ABILITY; + para.data = this; + + // wait for callback. + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_GET_CAMERA_ABILITY, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Get camera ability ipc transmission failed. (ret=%d)", ret); + } + // find cameraAbility again. + iter = deviceAbilityMap_.find(cameraId); + if (iter != deviceAbilityMap_.end()) { + return iter->second; + } + MEDIA_ERR_LOG("Get cameraAbility of camera %s from cameraService failded", cameraId.c_str()); + return nullptr; +} + +CameraInfo *CameraServiceClient::GetCameraInfo(string &cameraId) +{ + std::map::iterator iter = deviceInfoMap_.find(cameraId); + if (iter != deviceInfoMap_.end()) { + return iter->second; + } + cameraIdForInfo = cameraId; + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + IpcIoPushString(&io, cameraId.c_str()); + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_GET_CAMERA_INFO; + para.data = this; + // wait for callback. + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_GET_CAMERA_INFO, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Get camera info ipc transmission failed. (ret=%d)", ret); + } + + iter = deviceInfoMap_.find(cameraId); + if (iter != deviceInfoMap_.end()) { + return iter->second; + } + MEDIA_ERR_LOG("Get cameraInfo of camera %s from cameraService failded", cameraId.c_str()); + return nullptr; +} + +int32_t CameraServiceClient::ServiceClientCallback(const IpcContext* context, void *ipcMsg, IpcIo *io, void *arg) +{ + if (ipcMsg == nullptr) { + MEDIA_ERR_LOG("call back error, ipcMsg is null\n"); + return MEDIA_ERR; + } + if (arg == nullptr) { + MEDIA_ERR_LOG("call back error, arg is null\n"); + return MEDIA_ERR; + } + CallBackPara* para = static_cast(arg); + CameraServiceClient *client = static_cast(para->data); + uint32_t funcId; + (void)GetCode(ipcMsg, &funcId); + MEDIA_INFO_LOG("ServiceClientCallback, funcId=%d", funcId); + switch (funcId) { + case ON_CAMERA_STATUS_CHANGE: { + CameraServiceCallback::CameraStauts cameraStatus = + static_cast(IpcIoPopInt32(io)); + string cameraId = para->cameraId; + client->cameraServiceCb_->OnCameraStatusChange(cameraId, cameraStatus); + break; + } + default: { + MEDIA_ERR_LOG("unsupport funId\n"); + break; + } + } + client->cameraClient_->ClearIpcMsg(ipcMsg); + return MEDIA_OK; +} + +int32_t CameraServiceClient::SetCameraMode(uint8_t modeIndex) +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + IpcIoPushUint8(&io, modeIndex); + CallBackPara para = {}; + para.funcId = CAMERA_SERVER_SET_CAMERA_MODE_NUM; + para.data = this; + uint32_t ret = proxy_->Invoke(proxy_, CAMERA_SERVER_SET_CAMERA_MODE_NUM, &io, ¶, Callback); + if (ret != 0) { + MEDIA_ERR_LOG("Set camera mode failed.(ret=%d)", ret); + return ret; + } + return this->ret_; +} + +void CameraServiceClient::CreateCamera(string cameraId) +{ + para_ = new (nothrow) CallBackPara; + if (para_ == nullptr) { + MEDIA_ERR_LOG("para_ is null, failed."); + return; + } + para_->cameraId = cameraId; + para_->data = this; + int32_t ret = RegisterIpcCallback(ServiceClientCallback, 0, IPC_WAIT_FOREVER, &sid_, para_); + if (ret != LITEIPC_OK) { + MEDIA_ERR_LOG("RegisteIpcCallback failed, (ret=%d).", ret); + return; + } + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1); + IpcIoPushString(&io, cameraId.c_str()); + IpcIoPushSvc(&io, &sid_); + uint32_t ans = proxy_->Invoke(proxy_, CAMERA_SERVER_CREATE_CAMERA, &io, para_, Callback); + if (ans != 0) { + MEDIA_ERR_LOG("Create camera ipc transmission failed. (ret=%d)", ans); + } +} +} } \ No newline at end of file diff --git a/services/BUILD.gn b/services/BUILD.gn index 6f79bcf..d9d04bc 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -13,12 +13,17 @@ import("//build/lite/config/component/lite_component.gni") shared_library("camera_server") { sources = [ - "./src/camera_device.cpp", - "./src/camera_server.cpp", - "./src/camera_service.cpp", - "./src/samgr_camera.cpp", + "impl/src/camera_device.cpp", + "impl/src/camera_service.cpp", + "server/src/camera_server.cpp", + "server/src/samgr_camera.cpp", ] - include_dirs = [ "./include" ] + + include_dirs = [ + "//foundation/multimedia/camera_lite/services/impl/include", + "//foundation/multimedia/camera_lite/services/server/include", + ] + public_configs = [ ":external_camera_server_library" ] ldflags = [ "-lstdc++", diff --git a/services/include/camera_device.h b/services/impl/include/camera_device.h old mode 100755 new mode 100644 similarity index 96% rename from services/include/camera_device.h rename to services/impl/include/camera_device.h index 799a8b1..b3c13f2 --- a/services/include/camera_device.h +++ b/services/impl/include/camera_device.h @@ -1,126 +1,126 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * 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. - */ - -#ifndef OHOS_CAMERA_DEVICE_H -#define OHOS_CAMERA_DEVICE_H - -#include -#include -#include - -#include "camera_ability.h" -#include "camera_config.h" -#include "codec_type.h" -#include "frame_config.h" -#include "surface.h" -using namespace std; -namespace OHOS { -namespace Media { -enum LoopState { - LOOP_IDLE, - LOOP_READY, - LOOP_LOOPING, - LOOP_STOP, - LOOP_ERROR, -}; -const int32_t RECODER_MAX_NUM = 2; -class DeviceAssistant { -public: - std::thread *thrd_ = nullptr; - LoopState state_ = LOOP_IDLE; - FrameConfig *fc_ = nullptr; - uint32_t cameraId_; - uint32_t streamId_; - - virtual int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) - { - return -1; - } - virtual int32_t Start(uint32_t streamId) - { - return -1; - } - virtual int32_t Stop() - { - return -1; - } -}; - -class RecordAssistant : public DeviceAssistant { -public: - int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override; - int32_t Start(uint32_t streamId) override; - int32_t Stop() override; - - vector vencHdls_; - vector> vencSurfaces_; - static int OnVencBufferAvailble(UINTPTR hComponent, UINTPTR dataIn, OutputInfo *buffer); - static CodecCallback recordCodecCb_; - int32_t streamIdNum_[RECODER_MAX_NUM] = {-1, -1}; -}; - -class PreviewAssistant : public DeviceAssistant { -public: - int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override; - int32_t Start(uint32_t streamId) override; - virtual int32_t Stop() override; - Surface *capSurface_ = nullptr; -private: - pthread_t threadId; - static void *YuvCopyProcess(void *arg); -}; - -class CaptureAssistant : public DeviceAssistant { - int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override; - int32_t Start(uint32_t streamId) override; - virtual int32_t Stop() override; - CODEC_HANDLETYPE vencHdl_ = nullptr; - Surface *capSurface_ = nullptr; -}; - -class CallbackAssistant : public DeviceAssistant { -public: - int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override; - int32_t Start(uint32_t streamId) override; - virtual int32_t Stop() override; - Surface *capSurface_ = nullptr; -private: - pthread_t threadId; - static void *StreamCopyProcess(void *arg); -}; - -class CameraDevice { -public: - CameraDevice(); - CameraDevice(uint32_t cameraId); - virtual ~CameraDevice(); - - int32_t Initialize(); - int32_t UnInitialize(); - int32_t SetCameraConfig(); - int32_t TriggerLoopingCapture(FrameConfig &fc, uint32_t *streamId); - void StopLoopingCapture(); - int32_t TriggerSingleCapture(FrameConfig &fc, uint32_t *streamId); - uint32_t GetCameraId(); -private: - uint32_t cameraId; - RecordAssistant recordAssistant_; - PreviewAssistant previewAssistant_; - CaptureAssistant captureAssistant_; - CallbackAssistant callbackAssistant_; -}; -} // namespace Media -} // namespace OHOS +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef OHOS_CAMERA_DEVICE_H +#define OHOS_CAMERA_DEVICE_H + +#include +#include +#include + +#include "camera_ability.h" +#include "camera_config.h" +#include "codec_type.h" +#include "frame_config.h" +#include "surface.h" +using namespace std; +namespace OHOS { +namespace Media { +enum LoopState { + LOOP_IDLE, + LOOP_READY, + LOOP_LOOPING, + LOOP_STOP, + LOOP_ERROR, +}; +const int32_t RECODER_MAX_NUM = 2; +class DeviceAssistant { +public: + std::thread *thrd_ = nullptr; + LoopState state_ = LOOP_IDLE; + FrameConfig *fc_ = nullptr; + uint32_t cameraId_; + uint32_t streamId_; + + virtual int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) + { + return -1; + } + virtual int32_t Start(uint32_t streamId) + { + return -1; + } + virtual int32_t Stop() + { + return -1; + } +}; + +class RecordAssistant : public DeviceAssistant { +public: + int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override; + int32_t Start(uint32_t streamId) override; + int32_t Stop() override; + + vector vencHdls_; + vector> vencSurfaces_; + static int OnVencBufferAvailble(UINTPTR hComponent, UINTPTR dataIn, OutputInfo *buffer); + static CodecCallback recordCodecCb_; + int32_t streamIdNum_[RECODER_MAX_NUM] = {-1, -1}; +}; + +class PreviewAssistant : public DeviceAssistant { +public: + int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override; + int32_t Start(uint32_t streamId) override; + virtual int32_t Stop() override; + Surface *capSurface_ = nullptr; +private: + pthread_t threadId; + static void *YuvCopyProcess(void *arg); +}; + +class CaptureAssistant : public DeviceAssistant { + int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override; + int32_t Start(uint32_t streamId) override; + virtual int32_t Stop() override; + CODEC_HANDLETYPE vencHdl_ = nullptr; + Surface *capSurface_ = nullptr; +}; + +class CallbackAssistant : public DeviceAssistant { +public: + int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override; + int32_t Start(uint32_t streamId) override; + virtual int32_t Stop() override; + Surface *capSurface_ = nullptr; +private: + pthread_t threadId; + static void *StreamCopyProcess(void *arg); +}; + +class CameraDevice { +public: + CameraDevice(); + CameraDevice(uint32_t cameraId); + virtual ~CameraDevice(); + + int32_t Initialize(); + int32_t UnInitialize(); + int32_t SetCameraConfig(); + int32_t TriggerLoopingCapture(FrameConfig &fc, uint32_t *streamId); + void StopLoopingCapture(); + int32_t TriggerSingleCapture(FrameConfig &fc, uint32_t *streamId); + uint32_t GetCameraId(); +private: + uint32_t cameraId; + RecordAssistant recordAssistant_; + PreviewAssistant previewAssistant_; + CaptureAssistant captureAssistant_; + CallbackAssistant callbackAssistant_; +}; +} // namespace Media +} // namespace OHOS #endif // OHOS_CAMERA_DEVICE_H \ No newline at end of file diff --git a/services/include/camera_service.h b/services/impl/include/camera_service.h old mode 100755 new mode 100644 similarity index 97% rename from services/include/camera_service.h rename to services/impl/include/camera_service.h index 5a200cb..33c6401 --- a/services/include/camera_service.h +++ b/services/impl/include/camera_service.h @@ -1,50 +1,50 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * 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. - */ -#ifndef OHOS_CAMERA_SERVICE_H -#define OHOS_CAMERA_SERVICE_H - -#include "camera_device.h" -#include "camera_service_callback.h" -#include "camera_ability.h" -#include "camera_info_impl.h" - -namespace OHOS { -namespace Media { -class CameraService { -public: - ~CameraService(); - static CameraService *GetInstance(); - - void Initialize(); - CameraAbility *GetCameraAbility(std::string &cameraId); - CameraInfo *GetCameraInfo(std::string &cameraId); - CameraDevice *GetCameraDevice(std::string &cameraId); - int32_t CreateCamera(std::string cameraId); - int32_t CloseCamera(string cameraId); - list GetCameraIdList(); - uint8_t GetCameraModeNum(); - int32_t SetCameraMode(uint8_t modeIndex); -private: - CameraService(); - int32_t InitCameraDevices(); - std::map deviceMap_; - std::map deviceAbilityMap_; - std::map deviceInfoMap_; - CameraServiceCallback *cameraServiceCb_ = nullptr; -}; -} // namespace Media -} // namespace OHOS - +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * 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. + */ +#ifndef OHOS_CAMERA_SERVICE_H +#define OHOS_CAMERA_SERVICE_H + +#include "camera_device.h" +#include "camera_service_callback.h" +#include "camera_ability.h" +#include "camera_info_impl.h" + +namespace OHOS { +namespace Media { +class CameraService { +public: + ~CameraService(); + static CameraService *GetInstance(); + + void Initialize(); + CameraAbility *GetCameraAbility(std::string &cameraId); + CameraInfo *GetCameraInfo(std::string &cameraId); + CameraDevice *GetCameraDevice(std::string &cameraId); + int32_t CreateCamera(std::string cameraId); + int32_t CloseCamera(string cameraId); + list GetCameraIdList(); + uint8_t GetCameraModeNum(); + int32_t SetCameraMode(uint8_t modeIndex); +private: + CameraService(); + int32_t InitCameraDevices(); + std::map deviceMap_; + std::map deviceAbilityMap_; + std::map deviceInfoMap_; + CameraServiceCallback *cameraServiceCb_ = nullptr; +}; +} // namespace Media +} // namespace OHOS + #endif // OHOS_CAMERA_SERVICE_H \ No newline at end of file diff --git a/services/src/camera_device.cpp b/services/impl/src/camera_device.cpp old mode 100755 new mode 100644 similarity index 96% rename from services/src/camera_device.cpp rename to services/impl/src/camera_device.cpp index f3844fa..60e33b7 --- a/services/src/camera_device.cpp +++ b/services/impl/src/camera_device.cpp @@ -1,853 +1,853 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * 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. - */ - -#include "camera_device.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include "codec_interface.h" -#include "display_layer.h" -#include "hal_camera.h" -#include "media_log.h" -#include "meta_data.h" -#include "securec.h" - -#include -#include -#include - -using namespace OHOS; -using namespace OHOS::Media; -using namespace std; - -/** Indicates that the current frame is an Instantaneous Decoder Refresh (IDR) frame. */ -const int32_t KEY_IS_SYNC_FRAME = 1; -/** Indicates the frame timestamp. */ -const int32_t KEY_TIME_US = 2; - -const int32_t IMAGE_WIDTH = 3; // "DATA_PIX_FORMAT" -const int32_t IMAGE_HEIGHT = 4; // "DATA_PIX_FORMAT" -const int32_t IMAGE_SIZE = 5; // "DATA_PIX_FORMAT" -const int32_t DELAY_TIME_ONE_FRAME = 30000; -const int32_t VIDEO_MAX_NUM = 2; // "video max num" -const int32_t INVALID_STREAM_ID = -1; - -namespace OHOS { -namespace Media { -extern Surface *g_surface; -inline PicSize Convert2CodecSize(int32_t width, int32_t height) -{ - struct SizeMap { - PicSize res_; - uint32_t width_; - uint32_t height_; - }; - static SizeMap sizeMap[] = { - {RESOLUTION_CIF, 352, 288}, {RESOLUTION_360P, 640, 360}, {RESOLUTION_D1_PAL, 720, 576}, - {RESOLUTION_D1_NTSC, 720, 480}, {RESOLUTION_720P, 1280, 720}, {RESOLUTION_1080P, 1920, 1080}, - {RESOLUTION_2560X1440, 2560, 1440}, {RESOLUTION_2592X1520, 2592, 1520}, {RESOLUTION_2592X1536, 2592, 1536}, - {RESOLUTION_2592X1944, 2592, 1944}, {RESOLUTION_2688X1536, 2688, 1536}, {RESOLUTION_2716X1524, 2716, 1524}, - {RESOLUTION_3840X2160, 3840, 2160}, {RESOLUTION_4096X2160, 4096, 2160}, {RESOLUTION_3000X3000, 3000, 3000}, - {RESOLUTION_4000X3000, 4000, 3000}, {RESOLUTION_7680X4320, 7680, 4320}, {RESOLUTION_3840X8640, 3840, 8640} - }; - - for (uint32_t i = 0; i < sizeof(sizeMap) / sizeof(SizeMap); i++) { - if (sizeMap[i].width_ == width && sizeMap[i].height_ == height) { - return sizeMap[i].res_; - } - } - return RESOLUTION_INVALID; -} - -AvCodecMime ConverFormat(ImageFormat format) -{ - if (format == FORMAT_JPEG) { - return MEDIA_MIMETYPE_IMAGE_JPEG; - } else if (format == FORMAT_AVC) { - return MEDIA_MIMETYPE_VIDEO_AVC; - } else if (format == FORMAT_HEVC) { - return MEDIA_MIMETYPE_VIDEO_HEVC; - } else { - return MEDIA_MIMETYPE_INVALID; - } -} - -static int32_t SetVencSource(CODEC_HANDLETYPE codecHdl, uint32_t deviceId) -{ - Param param = {.key = KEY_DEVICE_ID, .val = (void *)&deviceId, .size = sizeof(uint32_t)}; - int32_t ret = CodecSetParameter(codecHdl, ¶m, 1); - if (ret != 0) { - MEDIA_ERR_LOG("Set enc source failed.(ret=%d)", ret); - return ret; - } - return MEDIA_OK; -} - -static uint32_t GetDefaultBitrate(PicSize size) -{ - uint32_t rate; /* auto calc bitrate if set 0 */ - if (size == RESOLUTION_360P) { - rate = 0x800; /* 2048kbps */ - } else if (size == RESOLUTION_720P) { - rate = 0x400; /* 1024kbps */ - } else if (size >= RESOLUTION_2560X1440 && size <= RESOLUTION_2716X1524) { - rate = 0x1800; /* 6144kbps */ - } else if (size == RESOLUTION_3840X2160 || size == RESOLUTION_4096X2160) { - rate = 0xa000; /* 40960kbps */ - } else { - rate = 0x0; - } - return rate; -} - -static int32_t CameraCreateVideoEnc(FrameConfig &fc, - StreamAttr stream, - uint32_t srcDev, - CODEC_HANDLETYPE *codecHdl) -{ - const char *name = "codec.video.hardware.encoder"; - const uint32_t maxParamNum = 10; - uint32_t paramIndex = 0; - Param param[maxParamNum]; - - CodecType domainKind = VIDEO_ENCODER; - param[paramIndex].key = KEY_CODEC_TYPE; - param[paramIndex].val = &domainKind; - param[paramIndex].size = sizeof(CodecType); - paramIndex++; - - AvCodecMime codecMime = ConverFormat(stream.format); - param[paramIndex].key = KEY_MIMETYPE; - param[paramIndex].val = &codecMime; - param[paramIndex].size = sizeof(AvCodecMime); - paramIndex++; - - VenCodeRcMode rcMode = VENCOD_RC_CBR; - param[paramIndex].key = KEY_VIDEO_RC_MODE; - param[paramIndex].val = &rcMode; - param[paramIndex].size = sizeof(VenCodeRcMode); - paramIndex++; - - VenCodeGopMode gopMode = VENCOD_GOPMODE_NORMALP; - param[paramIndex].key = KEY_VIDEO_GOP_MODE; - param[paramIndex].val = &gopMode; - param[paramIndex].size = sizeof(VenCodeGopMode); - paramIndex++; - - Profile profile = HEVC_MAIN_PROFILE; - param[paramIndex].key = KEY_VIDEO_PROFILE; - param[paramIndex].val = &profile; - param[paramIndex].size = sizeof(Profile); - paramIndex++; - -#ifdef __LINUX__ - PicSize picSize = Convert2CodecSize(g_surface->GetWidth(), g_surface->GetHeight()); -#else - PicSize picSize = Convert2CodecSize(stream.width, stream.height); -#endif - - MEDIA_DEBUG_LOG("picSize=%d", picSize); - param[paramIndex].key = KEY_VIDEO_PIC_SIZE; - param[paramIndex].val = &picSize; - param[paramIndex].size = sizeof(PicSize); - paramIndex++; - - uint32_t frameRate = stream.fps; - MEDIA_DEBUG_LOG("frameRate=%u", frameRate); - param[paramIndex].key = KEY_VIDEO_FRAME_RATE; - param[paramIndex].val = &frameRate; - param[paramIndex].size = sizeof(uint32_t); - paramIndex++; - - uint32_t bitRate = GetDefaultBitrate(picSize); - MEDIA_DEBUG_LOG("bitRate=%u kbps", bitRate); - param[paramIndex].key = KEY_BITRATE; - param[paramIndex].val = &bitRate; - param[paramIndex].size = sizeof(uint32_t); - paramIndex++; - - int32_t ret = CodecCreate(name, param, paramIndex, codecHdl); - if (ret != 0) { - MEDIA_ERR_LOG("Create video encoder failed."); - return MEDIA_ERR; - } - - ret = SetVencSource(*codecHdl, srcDev); - if (ret != 0) { - CodecDestroy(codecHdl); - return MEDIA_ERR; - } - - return MEDIA_OK; -} - -static int32_t CameraCreateJpegEnc(FrameConfig &fc, StreamAttr stream, uint32_t srcDev, CODEC_HANDLETYPE *codecHdl) -{ - const char *videoEncName = "codec.jpeg.hardware.encoder"; - const uint32_t maxParamNum = 5; - Param param[maxParamNum]; - uint32_t paramIndex = 0; - - CodecType domainKind = VIDEO_ENCODER; - param[paramIndex].key = KEY_CODEC_TYPE; - param[paramIndex].val = &domainKind; - param[paramIndex].size = sizeof(CodecType); - paramIndex++; - - AvCodecMime codecMime = ConverFormat(stream.format); - param[paramIndex].key = KEY_MIMETYPE; - param[paramIndex].val = &codecMime; - param[paramIndex].size = sizeof(AvCodecMime); - paramIndex++; - - auto surfaceList = fc.GetSurfaces(); - Surface *surface = surfaceList.front(); - - std::cout<<"------2: CameraCreateJpegEnc: surface width and height: " - <GetWidth()<<", "<GetHeight()<GetWidth(), surface->GetHeight()); - param[paramIndex].key = KEY_VIDEO_PIC_SIZE; - param[paramIndex].val = &picSize; - param[paramIndex].size = sizeof(PicSize); - paramIndex++; - - int32_t ret = CodecCreate(videoEncName, param, paramIndex, codecHdl); - if (ret != 0) { - return MEDIA_ERR; - } - int32_t qfactor = -1; - fc.GetParameter(PARAM_KEY_IMAGE_ENCODE_QFACTOR, qfactor); - if (qfactor != -1) { - Param jpegParam = { - .key = KEY_IMAGE_Q_FACTOR, - .val = &qfactor, - .size = sizeof(qfactor) - }; - ret = CodecSetParameter(*codecHdl, &jpegParam, 1); - if (ret != 0) { - MEDIA_ERR_LOG("CodecSetParameter set jpeg qfactor failed.(ret=%u)", ret); - } - } - - ret = SetVencSource(*codecHdl, srcDev); - if (ret != 0) { - MEDIA_ERR_LOG("Set video encoder source failed."); - CodecDestroy(*codecHdl); - return MEDIA_ERR; - } - - return MEDIA_OK; -} - -static int32_t CopyCodecOutput(void *dst, uint32_t *size, OutputInfo *buffer) -{ - char *dstBuf = reinterpret_cast(dst); - for (uint32_t i = 0; i < buffer->bufferCnt; i++) { - uint32_t packSize = buffer->buffers[i].length - buffer->buffers[i].offset; - errno_t ret = memcpy_s(dstBuf, *size, buffer->buffers[i].addr + buffer->buffers[i].offset, packSize); - if (ret != EOK) { - return MEDIA_ERR; - } - *size -= packSize; - dstBuf += packSize; - } - return MEDIA_OK; -} - -static void StreamAttrInitialize(StreamAttr *streamAttr, Surface *surface, - StreamType streamType, FrameConfig &fc) -{ - if (streamAttr == nullptr || surface == nullptr) { - return; - } - memset_s(streamAttr, sizeof(StreamAttr), 0, sizeof(StreamAttr)); - streamAttr->type = streamType; - fc.GetParameter(CAM_IMAGE_FORMAT, streamAttr->format); - streamAttr->width = surface->GetWidth(); - streamAttr->height = surface->GetHeight(); - fc.GetParameter(CAM_FRAME_FPS, streamAttr->fps); - fc.GetParameter(CAM_IMAGE_INVERT_MODE, streamAttr->invertMode); - fc.GetParameter(CAM_IMAGE_CROP_RECT, streamAttr->crop); -} - -static ImageFormat Convert2HalImageFormat(uint32_t format) -{ - if (format == CAM_IMAGE_RAW12) { - return FORMAT_RGB_BAYER_12BPP; - } - return FORMAT_YVU420; -} - -int32_t RecordAssistant::OnVencBufferAvailble(UINTPTR hComponent, UINTPTR dataIn, OutputInfo *buffer) -{ - CODEC_HANDLETYPE hdl = reinterpret_cast(hComponent); - RecordAssistant *assistant = reinterpret_cast(dataIn); - list *surfaceList = nullptr; - for (uint32_t idx = 0; idx < assistant->vencHdls_.size(); idx++) { - if (assistant->vencHdls_[idx] == hdl) { - surfaceList = &(assistant->vencSurfaces_[idx]); - break; - } - } - if (surfaceList == nullptr || surfaceList->empty()) { - MEDIA_ERR_LOG("Encoder handle is illegal."); - return MEDIA_ERR; - } - int32_t ret = -1; - for (auto &surface : *surfaceList) { -#ifdef __LINUX__ - SurfaceBuffer *surfaceBuf = g_surface->RequestBuffer(); -#else - SurfaceBuffer *surfaceBuf = surface->RequestBuffer(); -#endif - if (surfaceBuf == nullptr) { - MEDIA_ERR_LOG("No available buffer in surface."); - break; - } -#ifdef __LINUX__ - uint32_t size = g_surface->GetSize(); -#else - uint32_t size = surface->GetSize(); -#endif - void *buf = surfaceBuf->GetVirAddr(); - if (buf == nullptr) { - MEDIA_ERR_LOG("Invalid buffer address."); - break; - } - ret = CopyCodecOutput(buf, &size, buffer); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG("No available buffer in surface."); -#ifdef __LINUX__ - g_surface->CancelBuffer(surfaceBuf); -#else - surface->CancelBuffer(surfaceBuf); -#endif - break; - } - surfaceBuf->SetInt32(KEY_IS_SYNC_FRAME, (((buffer->flag & STREAM_FLAG_KEYFRAME) == 0) ? 0 : 1)); - surfaceBuf->SetInt64(KEY_TIME_US, buffer->timeStamp); -#ifdef __LINUX__ - surfaceBuf->SetSize(g_surface->GetSize() - size); - if (g_surface->FlushBuffer(surfaceBuf) != 0) { - MEDIA_ERR_LOG("Flush surface failed."); - g_surface->CancelBuffer(surfaceBuf); - ret = -1; - break; - } -#else - surfaceBuf->SetSize(surface->GetSize() - size); - if (surface->FlushBuffer(surfaceBuf) != 0) { - MEDIA_ERR_LOG("Flush surface failed."); - surface->CancelBuffer(surfaceBuf); - ret = -1; - break; - } -#endif - } - if (CodecQueueOutput(hdl, buffer, 0, -1) != 0) { - MEDIA_ERR_LOG("Codec queue output failed."); - } - return ret; -} - -CodecCallback RecordAssistant::recordCodecCb_ = {nullptr, nullptr, RecordAssistant::OnVencBufferAvailble}; - -int32_t RecordAssistant::SetFrameConfig(FrameConfig &fc, uint32_t *streamId) -{ - fc_ = &fc; - auto surfaceList = fc.GetSurfaces(); - if (surfaceList.size() > VIDEO_MAX_NUM || surfaceList.size() == 0) { - MEDIA_ERR_LOG("the number of surface in frame config must 1 or 2 now.\n"); - return MEDIA_ERR; - } - uint32_t num = 0; - int32_t ret = MEDIA_OK; - for (auto &surface : surfaceList) { - CODEC_HANDLETYPE codecHdl = nullptr; - StreamAttr stream = {}; -#ifdef __LINUX__ - StreamAttrInitialize(&stream, g_surface, STREAM_VIDEO, fc); -#else - StreamAttrInitialize(&stream, surface, STREAM_VIDEO, fc); -#endif - ret = HalCameraStreamCreate(cameraId_, &stream, streamId); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG(" creat recorder stream failed."); - return MEDIA_ERR; - } - streamIdNum_[num] = *streamId; - num++; - - StreamInfo streamInfo; - streamInfo.type = STERAM_INFO_PRIVATE; - fc.GetVendorParameter(streamInfo.u.data, PRIVATE_TAG_LEN); - HalCameraStreamSetInfo(cameraId_, *streamId, &streamInfo); - - uint32_t deviceId = 0; - HalCameraGetDeviceId(cameraId_, *streamId, &deviceId); - ret = CameraCreateVideoEnc(fc, stream, deviceId, &codecHdl); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG("Cannot create suitble video encoder."); - return MEDIA_ERR; - } - ret = CodecSetCallback(codecHdl, &recordCodecCb_, reinterpret_cast(this)); - if (ret != 0) { - MEDIA_ERR_LOG("Set codec callback failed.(ret=%d)", ret); - CodecDestroy(codecHdl); - return MEDIA_ERR; - } - vencHdls_.emplace_back(codecHdl); -#ifdef __LINUX__ - list conList({g_surface}); -#else - list conList({surface}); -#endif - vencSurfaces_.emplace_back(conList); - } - state_ = LOOP_READY; - return MEDIA_OK; -} - -int32_t RecordAssistant::Start(uint32_t streamId) -{ - if (state_ != LOOP_READY) { - return MEDIA_ERR; - } - HalCameraStreamOn(cameraId_, streamId); - int32_t ret = MEDIA_OK; - int32_t i; - for (i = 0; static_cast(i) < vencHdls_.size(); i++) { - ret = CodecStart(vencHdls_[i]); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG("Video encoder start failed."); - ret = MEDIA_ERR; - break; - } - } - if (ret == MEDIA_ERR) { - /* rollback */ - for (; i >= 0; i--) { - CodecStop(vencHdls_[i]); - } - return MEDIA_ERR; - } - state_ = LOOP_LOOPING; - MEDIA_INFO_LOG("Start camera recording succeed."); - return MEDIA_OK; -} - -int32_t RecordAssistant::Stop() -{ - if (state_ != LOOP_LOOPING) { - return MEDIA_ERR; - } - for (uint32_t i = 0; i < vencHdls_.size(); i++) { - CodecStop(vencHdls_[i]); - CodecDestroy(vencHdls_[i]); - } - vencHdls_.clear(); - vencSurfaces_.clear(); - for (uint32_t i = 0; i < VIDEO_MAX_NUM; i++) { - if (streamIdNum_[i] != INVALID_STREAM_ID) { - HalCameraStreamOff(cameraId_, streamIdNum_[i]); - HalCameraStreamDestroy(cameraId_, streamIdNum_[i]); - } - streamIdNum_[i] = INVALID_STREAM_ID; - } - state_ = LOOP_STOP; - return MEDIA_OK; -} - -void* PreviewAssistant::YuvCopyProcess(void *arg) -{ - return nullptr; -} - -int32_t PreviewAssistant::SetFrameConfig(FrameConfig &fc, uint32_t *streamId) -{ - fc_ = &fc; - auto surfaceList = fc.GetSurfaces(); - if (surfaceList.size() != 1) { - MEDIA_ERR_LOG("Only support one surface in frame config now."); - return MEDIA_ERR; - } - Surface *surface = surfaceList.front(); - StreamAttr stream = {}; - StreamAttrInitialize(&stream, surface, STREAM_PREVIEW, fc); - int32_t ret = HalCameraStreamCreate(cameraId_, &stream, streamId); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG(" creat Preview stream failed."); - return MEDIA_ERR; - } - StreamInfo streamInfo; - streamInfo.type = STREAM_INFO_POS; - streamInfo.u.pos.x = std::stoi(surface->GetUserData(string("region_position_x"))); - streamInfo.u.pos.y = std::stoi(surface->GetUserData(string("region_position_y"))); - - HalCameraStreamSetInfo(cameraId_, *streamId, &streamInfo); - streamId_ = *streamId; - return MEDIA_OK; -} - -int32_t PreviewAssistant::Start(uint32_t streamId) -{ - if (state_ == LOOP_LOOPING) { - return MEDIA_ERR; - } - state_ = LOOP_LOOPING; - - int32_t retCode = pthread_create(&threadId, nullptr, YuvCopyProcess, this); - if (retCode != 0) { - MEDIA_ERR_LOG("fork thread YuvCopyProcess failed: %d.", retCode); - } - - int32_t ret = HalCameraStreamOn(cameraId_, streamId); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG("Preview start failed. (ret=%d)", ret); - Stop(); - return MEDIA_ERR; - } - return MEDIA_OK; -} - -int32_t PreviewAssistant::Stop() -{ - if (state_ != LOOP_LOOPING) { - return MEDIA_ERR; - } - state_ = LOOP_STOP; - pthread_join(threadId, NULL); - HalCameraStreamOff(cameraId_, streamId_); - HalCameraStreamDestroy(cameraId_, streamId_); - return MEDIA_OK; -} - -int32_t CaptureAssistant::SetFrameConfig(FrameConfig &fc, uint32_t *streamId) -{ - auto surfaceList = fc.GetSurfaces(); - if (surfaceList.size() != 1) { - MEDIA_ERR_LOG("Only support one surface in frame config now."); - return MEDIA_ERR; - } - if (surfaceList.empty()) { - MEDIA_ERR_LOG("Frame config with empty surface list."); - return MEDIA_ERR; - } - if (surfaceList.size() > 1) { - MEDIA_WARNING_LOG("Capture only fullfill the first surface in frame config."); - } - Surface *surface = surfaceList.front(); - - StreamAttr stream = {}; - StreamAttrInitialize(&stream, surface, STREAM_CAPTURE, fc); - - uint32_t deviceId = 0; - int32_t ret = HalCameraStreamCreate(cameraId_, &stream, streamId); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG(" creat capture stream failed."); - return MEDIA_ERR; - } - streamId_ = *streamId; - HalCameraGetDeviceId(cameraId_, *streamId, &deviceId); - ret = CameraCreateJpegEnc(fc, stream, deviceId, &vencHdl_); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG("Create capture venc failed."); - return MEDIA_ERR; - } - - capSurface_ = surface; - state_ = LOOP_READY; - return MEDIA_OK; -} - -/* Block method, waiting for capture completed */ -int32_t CaptureAssistant::Start(uint32_t streamId) -{ - int32_t retCode = MEDIA_ERR; - state_ = LOOP_LOOPING; - HalCameraStreamOn(cameraId_, streamId); - int pictures = capSurface_->GetQueueSize(); - int32_t ret = CodecStart(vencHdl_); - if (ret != 0) { - MEDIA_ERR_LOG("Start capture encoder failed.(ret=%d)", ret); - goto FREE_RESOURCE; - } - - do { - SurfaceBuffer *surfaceBuf = capSurface_->RequestBuffer(); - if (surfaceBuf == nullptr) { - MEDIA_ERR_LOG("No available buffer in surface."); - break; - } - - OutputInfo outInfo; - ret = CodecDequeueOutput(vencHdl_, 0, nullptr, &outInfo); - if (ret != 0) { - capSurface_->CancelBuffer(surfaceBuf); - MEDIA_ERR_LOG("Dequeue capture frame failed.(ret=%d)", ret); - break; - } - - uint32_t size = capSurface_->GetSize(); - void *buf = surfaceBuf->GetVirAddr(); - if (buf == nullptr) { - MEDIA_ERR_LOG("Invalid buffer address."); - break; - } - if (CopyCodecOutput(buf, &size, &outInfo) != MEDIA_OK) { - MEDIA_ERR_LOG("No available buffer in capSurface_."); - capSurface_->CancelBuffer(surfaceBuf); - break; - } - surfaceBuf->SetSize(capSurface_->GetSize() - size); - - if (capSurface_->FlushBuffer(surfaceBuf) != 0) { - MEDIA_ERR_LOG("Flush surface buffer failed."); - capSurface_->CancelBuffer(surfaceBuf); - break; - } - - CodecQueueOutput(vencHdl_, &outInfo, 0, -1); // 0:no timeout -1:no fd - retCode = MEDIA_OK; - } while (--pictures); - - CodecStop(vencHdl_); - -FREE_RESOURCE: - CodecDestroy(vencHdl_); - HalCameraStreamOff(cameraId_, streamId); - HalCameraStreamDestroy(cameraId_, streamId); - delete capSurface_; - capSurface_ = nullptr; - state_ = LOOP_STOP; - - return retCode; -} - -int32_t CaptureAssistant::Stop() -{ - MEDIA_DEBUG_LOG("No support method."); - return MEDIA_OK; -} - -int32_t CallbackAssistant::SetFrameConfig(FrameConfig &fc, uint32_t *streamId) -{ - fc_ = &fc; - auto surfaceList = fc.GetSurfaces(); - if (surfaceList.size() != 1) { - MEDIA_ERR_LOG("Only support one surface in frame config now."); - return MEDIA_ERR; - } - uint32_t imageFormat = 0; - fc.GetParameter(CAM_IMAGE_FORMAT, imageFormat); - ImageFormat halImageFormat = Convert2HalImageFormat(imageFormat); - MEDIA_INFO_LOG("Imageformat is %d", imageFormat); - Surface *surface = surfaceList.front(); - StreamAttr stream = {}; - StreamAttrInitialize(&stream, surface, STREAM_CALLBACK, fc); - stream.format = halImageFormat; - int32_t ret = HalCameraStreamCreate(cameraId_, &stream, streamId); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG(" creat callback stream failed."); - return MEDIA_ERR; - } - streamId_ = *streamId; - capSurface_ = surface; - state_ = LOOP_READY; - return MEDIA_OK; -} - -int32_t CallbackAssistant::Start(uint32_t streamId) -{ - if (state_ == LOOP_LOOPING) { - return MEDIA_ERR; - } - state_ = LOOP_LOOPING; - int32_t retCode = pthread_create(&threadId, nullptr, StreamCopyProcess, this); - if (retCode != 0) { - MEDIA_ERR_LOG("fork thread StreamCopyProcess failed: %d.", retCode); - } - HalCameraStreamOn(cameraId_, streamId); - return MEDIA_OK; -} - -void* CallbackAssistant::StreamCopyProcess(void *arg) -{ - CallbackAssistant *assistant = (CallbackAssistant *)arg; - if (assistant == nullptr) { - MEDIA_ERR_LOG("CallbackAssistant create failed."); - return nullptr; - } - if (assistant->capSurface_ == nullptr) { - MEDIA_ERR_LOG("capSurface_ is null.\n"); - return nullptr; - } - - int32_t ret; - HalBuffer streamBuffer; - (void)memset_s(&streamBuffer, sizeof(HalBuffer), 0, sizeof(HalBuffer)); - while (assistant->state_ == LOOP_LOOPING) { - SurfaceBuffer *surfaceBuf = assistant->capSurface_->RequestBuffer(); - if (surfaceBuf == nullptr) { - usleep(DELAY_TIME_ONE_FRAME); - continue; - } - - if (streamBuffer.size != 0x0) { - HalCameraQueueBuf(assistant->cameraId_, assistant->streamId_, &streamBuffer); - (void)memset_s(&streamBuffer, sizeof(HalBuffer), 0, sizeof(HalBuffer)); - } - streamBuffer.format = FORMAT_PRIVATE; - streamBuffer.size = assistant->capSurface_->GetSize(); - if (surfaceBuf->GetVirAddr() == NULL) { - MEDIA_ERR_LOG("Invalid buffer address."); - break; - } - streamBuffer.virAddr = surfaceBuf->GetVirAddr(); - - ret = HalCameraDequeueBuf(assistant->cameraId_, assistant->streamId_, &streamBuffer); - if (ret != MEDIA_OK) { - usleep(DELAY_TIME_ONE_FRAME); - continue; - } - - if (assistant->capSurface_->FlushBuffer(surfaceBuf) != 0) { - MEDIA_ERR_LOG("Flush surface failed."); - assistant->capSurface_->CancelBuffer(surfaceBuf); - break; - } - usleep(DELAY_TIME_ONE_FRAME); - } - if (streamBuffer.size != 0x0) { - HalCameraQueueBuf(assistant->cameraId_, assistant->streamId_, &streamBuffer); - } - MEDIA_DEBUG_LOG(" yuv thread joined \n"); - return nullptr; -} - -int32_t CallbackAssistant::Stop() -{ - if (state_ != LOOP_LOOPING) { - return MEDIA_ERR; - } - state_ = LOOP_STOP; - pthread_join(threadId, NULL); - HalCameraStreamOff(cameraId_, streamId_); - HalCameraStreamDestroy(cameraId_, streamId_); - return MEDIA_OK; -} - -CameraDevice::CameraDevice() {} -CameraDevice::CameraDevice(uint32_t cameraId) -{ - this->cameraId = cameraId; -} - -CameraDevice::~CameraDevice() {} - -int32_t CameraDevice::Initialize() -{ - // Need to be Refactored when delete config file - int32_t ret = CodecInit(); - if (ret != 0) { - MEDIA_ERR_LOG("Codec module init failed.(ret=%d)", ret); - return MEDIA_ERR; - } - MEDIA_INFO_LOG("Codec module init succeed."); - captureAssistant_.state_ = LOOP_READY; - previewAssistant_.state_ = LOOP_READY; - recordAssistant_.state_ = LOOP_READY; - callbackAssistant_.state_ = LOOP_READY; - captureAssistant_.cameraId_ = cameraId; - previewAssistant_.cameraId_ = cameraId; - recordAssistant_.cameraId_ = cameraId; - callbackAssistant_.cameraId_ = cameraId; - return MEDIA_OK; -} - -int32_t CameraDevice::UnInitialize() -{ - return MEDIA_OK; -} - -int32_t CameraDevice::TriggerLoopingCapture(FrameConfig &fc, uint32_t *streamId) -{ - MEDIA_DEBUG_LOG("Camera device start looping capture."); - DeviceAssistant *assistant = nullptr; - int32_t fcType = fc.GetFrameConfigType(); - switch (fcType) { - case FRAME_CONFIG_RECORD: - assistant = &recordAssistant_; - break; - case FRAME_CONFIG_PREVIEW: - assistant = &previewAssistant_; - break; - case FRAME_CONFIG_CAPTURE: - assistant = &captureAssistant_; - break; - case FRAME_CONFIG_CALLBACK: - assistant = &callbackAssistant_; - break; - default: - break; - } - if (assistant == nullptr) { - MEDIA_ERR_LOG("Invalid frame config type.(type=%d)", fcType); - return MEDIA_ERR; - } - if (assistant->state_ == LOOP_IDLE || assistant->state_ == LOOP_LOOPING || assistant->state_ == LOOP_ERROR) { - MEDIA_ERR_LOG("Device state is %d, cannot start looping capture.", assistant->state_); - return MEDIA_ERR; - } - - int32_t ret = assistant->SetFrameConfig(fc, streamId); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG("Check and set frame config failed.(ret=%d)", ret); - return MEDIA_ERR; - } - - ret = assistant->Start(*streamId); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG("Start looping capture failed.(ret=%d)", ret); - return MEDIA_ERR; - } - return MEDIA_OK; -} - -void CameraDevice::StopLoopingCapture() -{ - MEDIA_INFO_LOG("Stop looping capture in camera_device.cpp"); - previewAssistant_.Stop(); - recordAssistant_.Stop(); - callbackAssistant_.Stop(); -} - -int32_t CameraDevice::TriggerSingleCapture(FrameConfig &fc, uint32_t *streamId) -{ - return TriggerLoopingCapture(fc, streamId); -} - -int32_t CameraDevice::SetCameraConfig() -{ - return MEDIA_OK; -} -} // namespace Media -} // namespace OHOS +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * 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. + */ + +#include "camera_device.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include "codec_interface.h" +#include "display_layer.h" +#include "hal_camera.h" +#include "media_log.h" +#include "meta_data.h" +#include "securec.h" + +#include +#include +#include + +using namespace OHOS; +using namespace OHOS::Media; +using namespace std; + +/** Indicates that the current frame is an Instantaneous Decoder Refresh (IDR) frame. */ +const int32_t KEY_IS_SYNC_FRAME = 1; +/** Indicates the frame timestamp. */ +const int32_t KEY_TIME_US = 2; + +const int32_t IMAGE_WIDTH = 3; // "DATA_PIX_FORMAT" +const int32_t IMAGE_HEIGHT = 4; // "DATA_PIX_FORMAT" +const int32_t IMAGE_SIZE = 5; // "DATA_PIX_FORMAT" +const int32_t DELAY_TIME_ONE_FRAME = 30000; +const int32_t VIDEO_MAX_NUM = 2; // "video max num" +const int32_t INVALID_STREAM_ID = -1; + +namespace OHOS { +namespace Media { +extern Surface *g_surface; +inline PicSize Convert2CodecSize(int32_t width, int32_t height) +{ + struct SizeMap { + PicSize res_; + uint32_t width_; + uint32_t height_; + }; + static SizeMap sizeMap[] = { + {RESOLUTION_CIF, 352, 288}, {RESOLUTION_360P, 640, 360}, {RESOLUTION_D1_PAL, 720, 576}, + {RESOLUTION_D1_NTSC, 720, 480}, {RESOLUTION_720P, 1280, 720}, {RESOLUTION_1080P, 1920, 1080}, + {RESOLUTION_2560X1440, 2560, 1440}, {RESOLUTION_2592X1520, 2592, 1520}, {RESOLUTION_2592X1536, 2592, 1536}, + {RESOLUTION_2592X1944, 2592, 1944}, {RESOLUTION_2688X1536, 2688, 1536}, {RESOLUTION_2716X1524, 2716, 1524}, + {RESOLUTION_3840X2160, 3840, 2160}, {RESOLUTION_4096X2160, 4096, 2160}, {RESOLUTION_3000X3000, 3000, 3000}, + {RESOLUTION_4000X3000, 4000, 3000}, {RESOLUTION_7680X4320, 7680, 4320}, {RESOLUTION_3840X8640, 3840, 8640} + }; + + for (uint32_t i = 0; i < sizeof(sizeMap) / sizeof(SizeMap); i++) { + if (sizeMap[i].width_ == width && sizeMap[i].height_ == height) { + return sizeMap[i].res_; + } + } + return RESOLUTION_INVALID; +} + +AvCodecMime ConverFormat(ImageFormat format) +{ + if (format == FORMAT_JPEG) { + return MEDIA_MIMETYPE_IMAGE_JPEG; + } else if (format == FORMAT_AVC) { + return MEDIA_MIMETYPE_VIDEO_AVC; + } else if (format == FORMAT_HEVC) { + return MEDIA_MIMETYPE_VIDEO_HEVC; + } else { + return MEDIA_MIMETYPE_INVALID; + } +} + +static int32_t SetVencSource(CODEC_HANDLETYPE codecHdl, uint32_t deviceId) +{ + Param param = {.key = KEY_DEVICE_ID, .val = (void *)&deviceId, .size = sizeof(uint32_t)}; + int32_t ret = CodecSetParameter(codecHdl, ¶m, 1); + if (ret != 0) { + MEDIA_ERR_LOG("Set enc source failed.(ret=%d)", ret); + return ret; + } + return MEDIA_OK; +} + +static uint32_t GetDefaultBitrate(PicSize size) +{ + uint32_t rate; /* auto calc bitrate if set 0 */ + if (size == RESOLUTION_360P) { + rate = 0x800; /* 2048kbps */ + } else if (size == RESOLUTION_720P) { + rate = 0x400; /* 1024kbps */ + } else if (size >= RESOLUTION_2560X1440 && size <= RESOLUTION_2716X1524) { + rate = 0x1800; /* 6144kbps */ + } else if (size == RESOLUTION_3840X2160 || size == RESOLUTION_4096X2160) { + rate = 0xa000; /* 40960kbps */ + } else { + rate = 0x0; + } + return rate; +} + +static int32_t CameraCreateVideoEnc(FrameConfig &fc, + StreamAttr stream, + uint32_t srcDev, + CODEC_HANDLETYPE *codecHdl) +{ + const char *name = "codec.video.hardware.encoder"; + const uint32_t maxParamNum = 10; + uint32_t paramIndex = 0; + Param param[maxParamNum]; + + CodecType domainKind = VIDEO_ENCODER; + param[paramIndex].key = KEY_CODEC_TYPE; + param[paramIndex].val = &domainKind; + param[paramIndex].size = sizeof(CodecType); + paramIndex++; + + AvCodecMime codecMime = ConverFormat(stream.format); + param[paramIndex].key = KEY_MIMETYPE; + param[paramIndex].val = &codecMime; + param[paramIndex].size = sizeof(AvCodecMime); + paramIndex++; + + VenCodeRcMode rcMode = VENCOD_RC_CBR; + param[paramIndex].key = KEY_VIDEO_RC_MODE; + param[paramIndex].val = &rcMode; + param[paramIndex].size = sizeof(VenCodeRcMode); + paramIndex++; + + VenCodeGopMode gopMode = VENCOD_GOPMODE_NORMALP; + param[paramIndex].key = KEY_VIDEO_GOP_MODE; + param[paramIndex].val = &gopMode; + param[paramIndex].size = sizeof(VenCodeGopMode); + paramIndex++; + + Profile profile = HEVC_MAIN_PROFILE; + param[paramIndex].key = KEY_VIDEO_PROFILE; + param[paramIndex].val = &profile; + param[paramIndex].size = sizeof(Profile); + paramIndex++; + +#ifdef __LINUX__ + PicSize picSize = Convert2CodecSize(g_surface->GetWidth(), g_surface->GetHeight()); +#else + PicSize picSize = Convert2CodecSize(stream.width, stream.height); +#endif + + MEDIA_DEBUG_LOG("picSize=%d", picSize); + param[paramIndex].key = KEY_VIDEO_PIC_SIZE; + param[paramIndex].val = &picSize; + param[paramIndex].size = sizeof(PicSize); + paramIndex++; + + uint32_t frameRate = stream.fps; + MEDIA_DEBUG_LOG("frameRate=%u", frameRate); + param[paramIndex].key = KEY_VIDEO_FRAME_RATE; + param[paramIndex].val = &frameRate; + param[paramIndex].size = sizeof(uint32_t); + paramIndex++; + + uint32_t bitRate = GetDefaultBitrate(picSize); + MEDIA_DEBUG_LOG("bitRate=%u kbps", bitRate); + param[paramIndex].key = KEY_BITRATE; + param[paramIndex].val = &bitRate; + param[paramIndex].size = sizeof(uint32_t); + paramIndex++; + + int32_t ret = CodecCreate(name, param, paramIndex, codecHdl); + if (ret != 0) { + MEDIA_ERR_LOG("Create video encoder failed."); + return MEDIA_ERR; + } + + ret = SetVencSource(*codecHdl, srcDev); + if (ret != 0) { + CodecDestroy(codecHdl); + return MEDIA_ERR; + } + + return MEDIA_OK; +} + +static int32_t CameraCreateJpegEnc(FrameConfig &fc, StreamAttr stream, uint32_t srcDev, CODEC_HANDLETYPE *codecHdl) +{ + const char *videoEncName = "codec.jpeg.hardware.encoder"; + const uint32_t maxParamNum = 5; + Param param[maxParamNum]; + uint32_t paramIndex = 0; + + CodecType domainKind = VIDEO_ENCODER; + param[paramIndex].key = KEY_CODEC_TYPE; + param[paramIndex].val = &domainKind; + param[paramIndex].size = sizeof(CodecType); + paramIndex++; + + AvCodecMime codecMime = ConverFormat(stream.format); + param[paramIndex].key = KEY_MIMETYPE; + param[paramIndex].val = &codecMime; + param[paramIndex].size = sizeof(AvCodecMime); + paramIndex++; + + auto surfaceList = fc.GetSurfaces(); + Surface *surface = surfaceList.front(); + + std::cout<<"------2: CameraCreateJpegEnc: surface width and height: " + <GetWidth()<<", "<GetHeight()<GetWidth(), surface->GetHeight()); + param[paramIndex].key = KEY_VIDEO_PIC_SIZE; + param[paramIndex].val = &picSize; + param[paramIndex].size = sizeof(PicSize); + paramIndex++; + + int32_t ret = CodecCreate(videoEncName, param, paramIndex, codecHdl); + if (ret != 0) { + return MEDIA_ERR; + } + int32_t qfactor = -1; + fc.GetParameter(PARAM_KEY_IMAGE_ENCODE_QFACTOR, qfactor); + if (qfactor != -1) { + Param jpegParam = { + .key = KEY_IMAGE_Q_FACTOR, + .val = &qfactor, + .size = sizeof(qfactor) + }; + ret = CodecSetParameter(*codecHdl, &jpegParam, 1); + if (ret != 0) { + MEDIA_ERR_LOG("CodecSetParameter set jpeg qfactor failed.(ret=%u)", ret); + } + } + + ret = SetVencSource(*codecHdl, srcDev); + if (ret != 0) { + MEDIA_ERR_LOG("Set video encoder source failed."); + CodecDestroy(*codecHdl); + return MEDIA_ERR; + } + + return MEDIA_OK; +} + +static int32_t CopyCodecOutput(void *dst, uint32_t *size, OutputInfo *buffer) +{ + char *dstBuf = reinterpret_cast(dst); + for (uint32_t i = 0; i < buffer->bufferCnt; i++) { + uint32_t packSize = buffer->buffers[i].length - buffer->buffers[i].offset; + errno_t ret = memcpy_s(dstBuf, *size, buffer->buffers[i].addr + buffer->buffers[i].offset, packSize); + if (ret != EOK) { + return MEDIA_ERR; + } + *size -= packSize; + dstBuf += packSize; + } + return MEDIA_OK; +} + +static void StreamAttrInitialize(StreamAttr *streamAttr, Surface *surface, + StreamType streamType, FrameConfig &fc) +{ + if (streamAttr == nullptr || surface == nullptr) { + return; + } + memset_s(streamAttr, sizeof(StreamAttr), 0, sizeof(StreamAttr)); + streamAttr->type = streamType; + fc.GetParameter(CAM_IMAGE_FORMAT, streamAttr->format); + streamAttr->width = surface->GetWidth(); + streamAttr->height = surface->GetHeight(); + fc.GetParameter(CAM_FRAME_FPS, streamAttr->fps); + fc.GetParameter(CAM_IMAGE_INVERT_MODE, streamAttr->invertMode); + fc.GetParameter(CAM_IMAGE_CROP_RECT, streamAttr->crop); +} + +static ImageFormat Convert2HalImageFormat(uint32_t format) +{ + if (format == CAM_IMAGE_RAW12) { + return FORMAT_RGB_BAYER_12BPP; + } + return FORMAT_YVU420; +} + +int32_t RecordAssistant::OnVencBufferAvailble(UINTPTR hComponent, UINTPTR dataIn, OutputInfo *buffer) +{ + CODEC_HANDLETYPE hdl = reinterpret_cast(hComponent); + RecordAssistant *assistant = reinterpret_cast(dataIn); + list *surfaceList = nullptr; + for (uint32_t idx = 0; idx < assistant->vencHdls_.size(); idx++) { + if (assistant->vencHdls_[idx] == hdl) { + surfaceList = &(assistant->vencSurfaces_[idx]); + break; + } + } + if (surfaceList == nullptr || surfaceList->empty()) { + MEDIA_ERR_LOG("Encoder handle is illegal."); + return MEDIA_ERR; + } + int32_t ret = -1; + for (auto &surface : *surfaceList) { +#ifdef __LINUX__ + SurfaceBuffer *surfaceBuf = g_surface->RequestBuffer(); +#else + SurfaceBuffer *surfaceBuf = surface->RequestBuffer(); +#endif + if (surfaceBuf == nullptr) { + MEDIA_ERR_LOG("No available buffer in surface."); + break; + } +#ifdef __LINUX__ + uint32_t size = g_surface->GetSize(); +#else + uint32_t size = surface->GetSize(); +#endif + void *buf = surfaceBuf->GetVirAddr(); + if (buf == nullptr) { + MEDIA_ERR_LOG("Invalid buffer address."); + break; + } + ret = CopyCodecOutput(buf, &size, buffer); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG("No available buffer in surface."); +#ifdef __LINUX__ + g_surface->CancelBuffer(surfaceBuf); +#else + surface->CancelBuffer(surfaceBuf); +#endif + break; + } + surfaceBuf->SetInt32(KEY_IS_SYNC_FRAME, (((buffer->flag & STREAM_FLAG_KEYFRAME) == 0) ? 0 : 1)); + surfaceBuf->SetInt64(KEY_TIME_US, buffer->timeStamp); +#ifdef __LINUX__ + surfaceBuf->SetSize(g_surface->GetSize() - size); + if (g_surface->FlushBuffer(surfaceBuf) != 0) { + MEDIA_ERR_LOG("Flush surface failed."); + g_surface->CancelBuffer(surfaceBuf); + ret = -1; + break; + } +#else + surfaceBuf->SetSize(surface->GetSize() - size); + if (surface->FlushBuffer(surfaceBuf) != 0) { + MEDIA_ERR_LOG("Flush surface failed."); + surface->CancelBuffer(surfaceBuf); + ret = -1; + break; + } +#endif + } + if (CodecQueueOutput(hdl, buffer, 0, -1) != 0) { + MEDIA_ERR_LOG("Codec queue output failed."); + } + return ret; +} + +CodecCallback RecordAssistant::recordCodecCb_ = {nullptr, nullptr, RecordAssistant::OnVencBufferAvailble}; + +int32_t RecordAssistant::SetFrameConfig(FrameConfig &fc, uint32_t *streamId) +{ + fc_ = &fc; + auto surfaceList = fc.GetSurfaces(); + if (surfaceList.size() > VIDEO_MAX_NUM || surfaceList.size() == 0) { + MEDIA_ERR_LOG("the number of surface in frame config must 1 or 2 now.\n"); + return MEDIA_ERR; + } + uint32_t num = 0; + int32_t ret = MEDIA_OK; + for (auto &surface : surfaceList) { + CODEC_HANDLETYPE codecHdl = nullptr; + StreamAttr stream = {}; +#ifdef __LINUX__ + StreamAttrInitialize(&stream, g_surface, STREAM_VIDEO, fc); +#else + StreamAttrInitialize(&stream, surface, STREAM_VIDEO, fc); +#endif + ret = HalCameraStreamCreate(cameraId_, &stream, streamId); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG(" creat recorder stream failed."); + return MEDIA_ERR; + } + streamIdNum_[num] = *streamId; + num++; + + StreamInfo streamInfo; + streamInfo.type = STERAM_INFO_PRIVATE; + fc.GetVendorParameter(streamInfo.u.data, PRIVATE_TAG_LEN); + HalCameraStreamSetInfo(cameraId_, *streamId, &streamInfo); + + uint32_t deviceId = 0; + HalCameraGetDeviceId(cameraId_, *streamId, &deviceId); + ret = CameraCreateVideoEnc(fc, stream, deviceId, &codecHdl); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG("Cannot create suitble video encoder."); + return MEDIA_ERR; + } + ret = CodecSetCallback(codecHdl, &recordCodecCb_, reinterpret_cast(this)); + if (ret != 0) { + MEDIA_ERR_LOG("Set codec callback failed.(ret=%d)", ret); + CodecDestroy(codecHdl); + return MEDIA_ERR; + } + vencHdls_.emplace_back(codecHdl); +#ifdef __LINUX__ + list conList({g_surface}); +#else + list conList({surface}); +#endif + vencSurfaces_.emplace_back(conList); + } + state_ = LOOP_READY; + return MEDIA_OK; +} + +int32_t RecordAssistant::Start(uint32_t streamId) +{ + if (state_ != LOOP_READY) { + return MEDIA_ERR; + } + HalCameraStreamOn(cameraId_, streamId); + int32_t ret = MEDIA_OK; + int32_t i; + for (i = 0; static_cast(i) < vencHdls_.size(); i++) { + ret = CodecStart(vencHdls_[i]); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG("Video encoder start failed."); + ret = MEDIA_ERR; + break; + } + } + if (ret == MEDIA_ERR) { + /* rollback */ + for (; i >= 0; i--) { + CodecStop(vencHdls_[i]); + } + return MEDIA_ERR; + } + state_ = LOOP_LOOPING; + MEDIA_INFO_LOG("Start camera recording succeed."); + return MEDIA_OK; +} + +int32_t RecordAssistant::Stop() +{ + if (state_ != LOOP_LOOPING) { + return MEDIA_ERR; + } + for (uint32_t i = 0; i < vencHdls_.size(); i++) { + CodecStop(vencHdls_[i]); + CodecDestroy(vencHdls_[i]); + } + vencHdls_.clear(); + vencSurfaces_.clear(); + for (uint32_t i = 0; i < VIDEO_MAX_NUM; i++) { + if (streamIdNum_[i] != INVALID_STREAM_ID) { + HalCameraStreamOff(cameraId_, streamIdNum_[i]); + HalCameraStreamDestroy(cameraId_, streamIdNum_[i]); + } + streamIdNum_[i] = INVALID_STREAM_ID; + } + state_ = LOOP_STOP; + return MEDIA_OK; +} + +void* PreviewAssistant::YuvCopyProcess(void *arg) +{ + return nullptr; +} + +int32_t PreviewAssistant::SetFrameConfig(FrameConfig &fc, uint32_t *streamId) +{ + fc_ = &fc; + auto surfaceList = fc.GetSurfaces(); + if (surfaceList.size() != 1) { + MEDIA_ERR_LOG("Only support one surface in frame config now."); + return MEDIA_ERR; + } + Surface *surface = surfaceList.front(); + StreamAttr stream = {}; + StreamAttrInitialize(&stream, surface, STREAM_PREVIEW, fc); + int32_t ret = HalCameraStreamCreate(cameraId_, &stream, streamId); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG(" creat Preview stream failed."); + return MEDIA_ERR; + } + StreamInfo streamInfo; + streamInfo.type = STREAM_INFO_POS; + streamInfo.u.pos.x = std::stoi(surface->GetUserData(string("region_position_x"))); + streamInfo.u.pos.y = std::stoi(surface->GetUserData(string("region_position_y"))); + + HalCameraStreamSetInfo(cameraId_, *streamId, &streamInfo); + streamId_ = *streamId; + return MEDIA_OK; +} + +int32_t PreviewAssistant::Start(uint32_t streamId) +{ + if (state_ == LOOP_LOOPING) { + return MEDIA_ERR; + } + state_ = LOOP_LOOPING; + + int32_t retCode = pthread_create(&threadId, nullptr, YuvCopyProcess, this); + if (retCode != 0) { + MEDIA_ERR_LOG("fork thread YuvCopyProcess failed: %d.", retCode); + } + + int32_t ret = HalCameraStreamOn(cameraId_, streamId); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG("Preview start failed. (ret=%d)", ret); + Stop(); + return MEDIA_ERR; + } + return MEDIA_OK; +} + +int32_t PreviewAssistant::Stop() +{ + if (state_ != LOOP_LOOPING) { + return MEDIA_ERR; + } + state_ = LOOP_STOP; + pthread_join(threadId, NULL); + HalCameraStreamOff(cameraId_, streamId_); + HalCameraStreamDestroy(cameraId_, streamId_); + return MEDIA_OK; +} + +int32_t CaptureAssistant::SetFrameConfig(FrameConfig &fc, uint32_t *streamId) +{ + auto surfaceList = fc.GetSurfaces(); + if (surfaceList.size() != 1) { + MEDIA_ERR_LOG("Only support one surface in frame config now."); + return MEDIA_ERR; + } + if (surfaceList.empty()) { + MEDIA_ERR_LOG("Frame config with empty surface list."); + return MEDIA_ERR; + } + if (surfaceList.size() > 1) { + MEDIA_WARNING_LOG("Capture only fullfill the first surface in frame config."); + } + Surface *surface = surfaceList.front(); + + StreamAttr stream = {}; + StreamAttrInitialize(&stream, surface, STREAM_CAPTURE, fc); + + uint32_t deviceId = 0; + int32_t ret = HalCameraStreamCreate(cameraId_, &stream, streamId); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG(" creat capture stream failed."); + return MEDIA_ERR; + } + streamId_ = *streamId; + HalCameraGetDeviceId(cameraId_, *streamId, &deviceId); + ret = CameraCreateJpegEnc(fc, stream, deviceId, &vencHdl_); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG("Create capture venc failed."); + return MEDIA_ERR; + } + + capSurface_ = surface; + state_ = LOOP_READY; + return MEDIA_OK; +} + +/* Block method, waiting for capture completed */ +int32_t CaptureAssistant::Start(uint32_t streamId) +{ + int32_t retCode = MEDIA_ERR; + state_ = LOOP_LOOPING; + HalCameraStreamOn(cameraId_, streamId); + int pictures = capSurface_->GetQueueSize(); + int32_t ret = CodecStart(vencHdl_); + if (ret != 0) { + MEDIA_ERR_LOG("Start capture encoder failed.(ret=%d)", ret); + goto FREE_RESOURCE; + } + + do { + SurfaceBuffer *surfaceBuf = capSurface_->RequestBuffer(); + if (surfaceBuf == nullptr) { + MEDIA_ERR_LOG("No available buffer in surface."); + break; + } + + OutputInfo outInfo; + ret = CodecDequeueOutput(vencHdl_, 0, nullptr, &outInfo); + if (ret != 0) { + capSurface_->CancelBuffer(surfaceBuf); + MEDIA_ERR_LOG("Dequeue capture frame failed.(ret=%d)", ret); + break; + } + + uint32_t size = capSurface_->GetSize(); + void *buf = surfaceBuf->GetVirAddr(); + if (buf == nullptr) { + MEDIA_ERR_LOG("Invalid buffer address."); + break; + } + if (CopyCodecOutput(buf, &size, &outInfo) != MEDIA_OK) { + MEDIA_ERR_LOG("No available buffer in capSurface_."); + capSurface_->CancelBuffer(surfaceBuf); + break; + } + surfaceBuf->SetSize(capSurface_->GetSize() - size); + + if (capSurface_->FlushBuffer(surfaceBuf) != 0) { + MEDIA_ERR_LOG("Flush surface buffer failed."); + capSurface_->CancelBuffer(surfaceBuf); + break; + } + + CodecQueueOutput(vencHdl_, &outInfo, 0, -1); // 0:no timeout -1:no fd + retCode = MEDIA_OK; + } while (--pictures); + + CodecStop(vencHdl_); + +FREE_RESOURCE: + CodecDestroy(vencHdl_); + HalCameraStreamOff(cameraId_, streamId); + HalCameraStreamDestroy(cameraId_, streamId); + delete capSurface_; + capSurface_ = nullptr; + state_ = LOOP_STOP; + + return retCode; +} + +int32_t CaptureAssistant::Stop() +{ + MEDIA_DEBUG_LOG("No support method."); + return MEDIA_OK; +} + +int32_t CallbackAssistant::SetFrameConfig(FrameConfig &fc, uint32_t *streamId) +{ + fc_ = &fc; + auto surfaceList = fc.GetSurfaces(); + if (surfaceList.size() != 1) { + MEDIA_ERR_LOG("Only support one surface in frame config now."); + return MEDIA_ERR; + } + uint32_t imageFormat = 0; + fc.GetParameter(CAM_IMAGE_FORMAT, imageFormat); + ImageFormat halImageFormat = Convert2HalImageFormat(imageFormat); + MEDIA_INFO_LOG("Imageformat is %d", imageFormat); + Surface *surface = surfaceList.front(); + StreamAttr stream = {}; + StreamAttrInitialize(&stream, surface, STREAM_CALLBACK, fc); + stream.format = halImageFormat; + int32_t ret = HalCameraStreamCreate(cameraId_, &stream, streamId); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG(" creat callback stream failed."); + return MEDIA_ERR; + } + streamId_ = *streamId; + capSurface_ = surface; + state_ = LOOP_READY; + return MEDIA_OK; +} + +int32_t CallbackAssistant::Start(uint32_t streamId) +{ + if (state_ == LOOP_LOOPING) { + return MEDIA_ERR; + } + state_ = LOOP_LOOPING; + int32_t retCode = pthread_create(&threadId, nullptr, StreamCopyProcess, this); + if (retCode != 0) { + MEDIA_ERR_LOG("fork thread StreamCopyProcess failed: %d.", retCode); + } + HalCameraStreamOn(cameraId_, streamId); + return MEDIA_OK; +} + +void* CallbackAssistant::StreamCopyProcess(void *arg) +{ + CallbackAssistant *assistant = (CallbackAssistant *)arg; + if (assistant == nullptr) { + MEDIA_ERR_LOG("CallbackAssistant create failed."); + return nullptr; + } + if (assistant->capSurface_ == nullptr) { + MEDIA_ERR_LOG("capSurface_ is null.\n"); + return nullptr; + } + + int32_t ret; + HalBuffer streamBuffer; + (void)memset_s(&streamBuffer, sizeof(HalBuffer), 0, sizeof(HalBuffer)); + while (assistant->state_ == LOOP_LOOPING) { + SurfaceBuffer *surfaceBuf = assistant->capSurface_->RequestBuffer(); + if (surfaceBuf == nullptr) { + usleep(DELAY_TIME_ONE_FRAME); + continue; + } + + if (streamBuffer.size != 0x0) { + HalCameraQueueBuf(assistant->cameraId_, assistant->streamId_, &streamBuffer); + (void)memset_s(&streamBuffer, sizeof(HalBuffer), 0, sizeof(HalBuffer)); + } + streamBuffer.format = FORMAT_PRIVATE; + streamBuffer.size = assistant->capSurface_->GetSize(); + if (surfaceBuf->GetVirAddr() == NULL) { + MEDIA_ERR_LOG("Invalid buffer address."); + break; + } + streamBuffer.virAddr = surfaceBuf->GetVirAddr(); + + ret = HalCameraDequeueBuf(assistant->cameraId_, assistant->streamId_, &streamBuffer); + if (ret != MEDIA_OK) { + usleep(DELAY_TIME_ONE_FRAME); + continue; + } + + if (assistant->capSurface_->FlushBuffer(surfaceBuf) != 0) { + MEDIA_ERR_LOG("Flush surface failed."); + assistant->capSurface_->CancelBuffer(surfaceBuf); + break; + } + usleep(DELAY_TIME_ONE_FRAME); + } + if (streamBuffer.size != 0x0) { + HalCameraQueueBuf(assistant->cameraId_, assistant->streamId_, &streamBuffer); + } + MEDIA_DEBUG_LOG(" yuv thread joined \n"); + return nullptr; +} + +int32_t CallbackAssistant::Stop() +{ + if (state_ != LOOP_LOOPING) { + return MEDIA_ERR; + } + state_ = LOOP_STOP; + pthread_join(threadId, NULL); + HalCameraStreamOff(cameraId_, streamId_); + HalCameraStreamDestroy(cameraId_, streamId_); + return MEDIA_OK; +} + +CameraDevice::CameraDevice() {} +CameraDevice::CameraDevice(uint32_t cameraId) +{ + this->cameraId = cameraId; +} + +CameraDevice::~CameraDevice() {} + +int32_t CameraDevice::Initialize() +{ + // Need to be Refactored when delete config file + int32_t ret = CodecInit(); + if (ret != 0) { + MEDIA_ERR_LOG("Codec module init failed.(ret=%d)", ret); + return MEDIA_ERR; + } + MEDIA_INFO_LOG("Codec module init succeed."); + captureAssistant_.state_ = LOOP_READY; + previewAssistant_.state_ = LOOP_READY; + recordAssistant_.state_ = LOOP_READY; + callbackAssistant_.state_ = LOOP_READY; + captureAssistant_.cameraId_ = cameraId; + previewAssistant_.cameraId_ = cameraId; + recordAssistant_.cameraId_ = cameraId; + callbackAssistant_.cameraId_ = cameraId; + return MEDIA_OK; +} + +int32_t CameraDevice::UnInitialize() +{ + return MEDIA_OK; +} + +int32_t CameraDevice::TriggerLoopingCapture(FrameConfig &fc, uint32_t *streamId) +{ + MEDIA_DEBUG_LOG("Camera device start looping capture."); + DeviceAssistant *assistant = nullptr; + int32_t fcType = fc.GetFrameConfigType(); + switch (fcType) { + case FRAME_CONFIG_RECORD: + assistant = &recordAssistant_; + break; + case FRAME_CONFIG_PREVIEW: + assistant = &previewAssistant_; + break; + case FRAME_CONFIG_CAPTURE: + assistant = &captureAssistant_; + break; + case FRAME_CONFIG_CALLBACK: + assistant = &callbackAssistant_; + break; + default: + break; + } + if (assistant == nullptr) { + MEDIA_ERR_LOG("Invalid frame config type.(type=%d)", fcType); + return MEDIA_ERR; + } + if (assistant->state_ == LOOP_IDLE || assistant->state_ == LOOP_LOOPING || assistant->state_ == LOOP_ERROR) { + MEDIA_ERR_LOG("Device state is %d, cannot start looping capture.", assistant->state_); + return MEDIA_ERR; + } + + int32_t ret = assistant->SetFrameConfig(fc, streamId); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG("Check and set frame config failed.(ret=%d)", ret); + return MEDIA_ERR; + } + + ret = assistant->Start(*streamId); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG("Start looping capture failed.(ret=%d)", ret); + return MEDIA_ERR; + } + return MEDIA_OK; +} + +void CameraDevice::StopLoopingCapture() +{ + MEDIA_INFO_LOG("Stop looping capture in camera_device.cpp"); + previewAssistant_.Stop(); + recordAssistant_.Stop(); + callbackAssistant_.Stop(); +} + +int32_t CameraDevice::TriggerSingleCapture(FrameConfig &fc, uint32_t *streamId) +{ + return TriggerLoopingCapture(fc, streamId); +} + +int32_t CameraDevice::SetCameraConfig() +{ + return MEDIA_OK; +} +} // namespace Media +} // namespace OHOS diff --git a/services/src/camera_service.cpp b/services/impl/src/camera_service.cpp old mode 100755 new mode 100644 similarity index 97% rename from services/src/camera_service.cpp rename to services/impl/src/camera_service.cpp index a2c9821..89d05f0 --- a/services/src/camera_service.cpp +++ b/services/impl/src/camera_service.cpp @@ -1,187 +1,187 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * 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. - */ -#include "camera_service.h" -#include "hal_camera.h" -#include "media_log.h" -#include "codec_interface.h" - -using namespace std; -namespace OHOS { -namespace Media { -CameraService::CameraService() {} - -CameraService::~CameraService() -{ - int32_t ret = HalCameraDeinit(); - if (ret != 0) { - MEDIA_ERR_LOG("HiCameraDeInit return failed ret(%d).", ret); - } -} - -CameraService *CameraService::GetInstance() -{ - static CameraService instance; - return &instance; -} - -void CameraService::Initialize() -{ - int32_t ret = HalCameraInit(); - if (ret != 0) { - MEDIA_ERR_LOG("HiCameraInit failed. ret(%d)", ret); - } -} - -CameraAbility *CameraService::GetCameraAbility(std::string &cameraId) -{ - std::map::iterator iter = deviceAbilityMap_.find(cameraId); - if (iter != deviceAbilityMap_.end()) { - return iter->second; - } - CameraAbility *ability = new (nothrow) CameraAbility; - if (ability == nullptr) { - return nullptr; - } - uint32_t streamCapNum; - StreamCap *streamCap = nullptr; - int32_t ret = HalCameraGetStreamCapNum(atoi(cameraId.c_str()), &streamCapNum); - streamCap = new StreamCap[streamCapNum]; - for (int pos = 0; pos < streamCapNum; pos++) { - streamCap[pos].type = CAP_DESC_ENUM; - } - ret = HalCameraGetStreamCap(atoi(cameraId.c_str()), streamCap, streamCapNum); - list range; - for (int pos = 0; pos < streamCapNum; pos++) { - CameraPicSize tmpSize = {.width = (uint32_t)streamCap[pos].u.formatEnum.width, - .height = (uint32_t)streamCap[pos].u.formatEnum.height}; - range.emplace_back(tmpSize); - } - ability->SetParameterRange(CAM_FORMAT_YVU420, range); - ability->SetParameterRange(CAM_FORMAT_JPEG, range); - ability->SetParameterRange(CAM_FORMAT_H264, range); - ability->SetParameterRange(CAM_FORMAT_H265, range); - AbilityInfo cameraAbility = {}; - HalCameraGetAbility(atoi(cameraId.c_str()), &cameraAbility); - list afModes; - for (int i = 0; i < cameraAbility.afModeNum; i++) { - afModes.emplace_back(cameraAbility.afModes[i]); - } - ability->SetParameterRange(CAM_AF_MODE, afModes); - list aeModes; - for (int i = 0; i < cameraAbility.aeModeNum; i++) { - aeModes.emplace_back(cameraAbility.aeModes[i]); - } - ability->SetParameterRange(CAM_AE_MODE, aeModes); - delete[] streamCap; - deviceAbilityMap_.insert(pair(cameraId, ability)); - return ability; -} - -CameraInfo *CameraService::GetCameraInfo(std::string &cameraId) -{ - std::map::iterator iter = deviceInfoMap_.find(cameraId); - if (iter != deviceInfoMap_.end()) { - return iter->second; - } - AbilityInfo deviceAbility; - int32_t ret = HalCameraGetAbility((uint32_t)std::atoi(cameraId.c_str()), &deviceAbility); - if (ret != MEDIA_OK) { - MEDIA_ERR_LOG("HalCameraGetAbility failed. ret(%d)", ret); - return nullptr; - } - CameraInfo *info = new (nothrow) CameraInfoImpl(deviceAbility.type, deviceAbility.orientation); - if (info == nullptr) { - return nullptr; - } - deviceInfoMap_.insert(pair(cameraId, info)); - return info; -} - -CameraDevice *CameraService::GetCameraDevice(std::string &cameraId) -{ - std::map::iterator iter = deviceMap_.find(cameraId); - if (iter != deviceMap_.end()) { - return iter->second; - } - return nullptr; -} - -list CameraService::GetCameraIdList() -{ - uint8_t camNum = 0; - HalCameraGetDeviceNum(&camNum); - uint32_t *cameraList = new uint32_t[camNum]; - HalCameraGetDeviceList(cameraList, camNum); - list cameraStrList; - for (uint32_t pos = 0; pos < camNum; pos++) { - cameraStrList.push_back(to_string(cameraList[pos])); - } - return cameraStrList; -} - -uint8_t CameraService::GetCameraModeNum() -{ - uint8_t num; - int32_t ret = HalCameraGetModeNum(&num); - if (ret == MEDIA_OK) { - return num; - } - return 0; -} - -int32_t CameraService::CreateCamera(string cameraId) -{ - int32_t ret = HalCameraDeviceOpen((uint32_t)std::atoi(cameraId.c_str())); - if (ret != 0) { - MEDIA_ERR_LOG("HalCameraDeviceOpen failed. ret(%d)", ret); - return CameraServiceCallback::CAMERA_STATUS_CREATE_FAILED; - } - CameraDevice *device = new (nothrow) CameraDevice((uint32_t)std::atoi(cameraId.c_str())); - if (device == nullptr) { - MEDIA_FATAL_LOG("New device object failed."); - return MEDIA_ERR; - } - if (device->Initialize() != MEDIA_OK) { - MEDIA_FATAL_LOG("device Initialize failed."); - delete device; - return MEDIA_ERR; - } - deviceMap_.insert(pair(cameraId, device)); - return CameraServiceCallback::CAMERA_STATUS_CREATED; -} - -int32_t CameraService::CloseCamera(string cameraId) -{ - CameraDevice *device = GetCameraDevice(cameraId); - if (device != NULL) { - device->StopLoopingCapture(); - deviceMap_.erase(cameraId); - } - int32_t ret = HalCameraDeviceClose((uint32_t)std::atoi(cameraId.c_str())); - if (ret != 0) { - MEDIA_ERR_LOG("HalCameraDeviceClose failed. ret(%d)", ret); - } - return CameraServiceCallback::CAMERA_STATUS_CLOSE; -} - -int32_t CameraService::SetCameraMode(uint8_t modeIndex) -{ - CodecDeinit(); - int32_t ret = HalCameraSetMode(modeIndex); - CodecInit(); - return ret; -} -} // namespace Media +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * 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. + */ +#include "camera_service.h" +#include "hal_camera.h" +#include "media_log.h" +#include "codec_interface.h" + +using namespace std; +namespace OHOS { +namespace Media { +CameraService::CameraService() {} + +CameraService::~CameraService() +{ + int32_t ret = HalCameraDeinit(); + if (ret != 0) { + MEDIA_ERR_LOG("HiCameraDeInit return failed ret(%d).", ret); + } +} + +CameraService *CameraService::GetInstance() +{ + static CameraService instance; + return &instance; +} + +void CameraService::Initialize() +{ + int32_t ret = HalCameraInit(); + if (ret != 0) { + MEDIA_ERR_LOG("HiCameraInit failed. ret(%d)", ret); + } +} + +CameraAbility *CameraService::GetCameraAbility(std::string &cameraId) +{ + std::map::iterator iter = deviceAbilityMap_.find(cameraId); + if (iter != deviceAbilityMap_.end()) { + return iter->second; + } + CameraAbility *ability = new (nothrow) CameraAbility; + if (ability == nullptr) { + return nullptr; + } + uint32_t streamCapNum; + StreamCap *streamCap = nullptr; + int32_t ret = HalCameraGetStreamCapNum(atoi(cameraId.c_str()), &streamCapNum); + streamCap = new StreamCap[streamCapNum]; + for (int pos = 0; pos < streamCapNum; pos++) { + streamCap[pos].type = CAP_DESC_ENUM; + } + ret = HalCameraGetStreamCap(atoi(cameraId.c_str()), streamCap, streamCapNum); + list range; + for (int pos = 0; pos < streamCapNum; pos++) { + CameraPicSize tmpSize = {.width = (uint32_t)streamCap[pos].u.formatEnum.width, + .height = (uint32_t)streamCap[pos].u.formatEnum.height}; + range.emplace_back(tmpSize); + } + ability->SetParameterRange(CAM_FORMAT_YVU420, range); + ability->SetParameterRange(CAM_FORMAT_JPEG, range); + ability->SetParameterRange(CAM_FORMAT_H264, range); + ability->SetParameterRange(CAM_FORMAT_H265, range); + AbilityInfo cameraAbility = {}; + HalCameraGetAbility(atoi(cameraId.c_str()), &cameraAbility); + list afModes; + for (int i = 0; i < cameraAbility.afModeNum; i++) { + afModes.emplace_back(cameraAbility.afModes[i]); + } + ability->SetParameterRange(CAM_AF_MODE, afModes); + list aeModes; + for (int i = 0; i < cameraAbility.aeModeNum; i++) { + aeModes.emplace_back(cameraAbility.aeModes[i]); + } + ability->SetParameterRange(CAM_AE_MODE, aeModes); + delete[] streamCap; + deviceAbilityMap_.insert(pair(cameraId, ability)); + return ability; +} + +CameraInfo *CameraService::GetCameraInfo(std::string &cameraId) +{ + std::map::iterator iter = deviceInfoMap_.find(cameraId); + if (iter != deviceInfoMap_.end()) { + return iter->second; + } + AbilityInfo deviceAbility; + int32_t ret = HalCameraGetAbility((uint32_t)std::atoi(cameraId.c_str()), &deviceAbility); + if (ret != MEDIA_OK) { + MEDIA_ERR_LOG("HalCameraGetAbility failed. ret(%d)", ret); + return nullptr; + } + CameraInfo *info = new (nothrow) CameraInfoImpl(deviceAbility.type, deviceAbility.orientation); + if (info == nullptr) { + return nullptr; + } + deviceInfoMap_.insert(pair(cameraId, info)); + return info; +} + +CameraDevice *CameraService::GetCameraDevice(std::string &cameraId) +{ + std::map::iterator iter = deviceMap_.find(cameraId); + if (iter != deviceMap_.end()) { + return iter->second; + } + return nullptr; +} + +list CameraService::GetCameraIdList() +{ + uint8_t camNum = 0; + HalCameraGetDeviceNum(&camNum); + uint32_t *cameraList = new uint32_t[camNum]; + HalCameraGetDeviceList(cameraList, camNum); + list cameraStrList; + for (uint32_t pos = 0; pos < camNum; pos++) { + cameraStrList.push_back(to_string(cameraList[pos])); + } + return cameraStrList; +} + +uint8_t CameraService::GetCameraModeNum() +{ + uint8_t num; + int32_t ret = HalCameraGetModeNum(&num); + if (ret == MEDIA_OK) { + return num; + } + return 0; +} + +int32_t CameraService::CreateCamera(string cameraId) +{ + int32_t ret = HalCameraDeviceOpen((uint32_t)std::atoi(cameraId.c_str())); + if (ret != 0) { + MEDIA_ERR_LOG("HalCameraDeviceOpen failed. ret(%d)", ret); + return CameraServiceCallback::CAMERA_STATUS_CREATE_FAILED; + } + CameraDevice *device = new (nothrow) CameraDevice((uint32_t)std::atoi(cameraId.c_str())); + if (device == nullptr) { + MEDIA_FATAL_LOG("New device object failed."); + return MEDIA_ERR; + } + if (device->Initialize() != MEDIA_OK) { + MEDIA_FATAL_LOG("device Initialize failed."); + delete device; + return MEDIA_ERR; + } + deviceMap_.insert(pair(cameraId, device)); + return CameraServiceCallback::CAMERA_STATUS_CREATED; +} + +int32_t CameraService::CloseCamera(string cameraId) +{ + CameraDevice *device = GetCameraDevice(cameraId); + if (device != NULL) { + device->StopLoopingCapture(); + deviceMap_.erase(cameraId); + } + int32_t ret = HalCameraDeviceClose((uint32_t)std::atoi(cameraId.c_str())); + if (ret != 0) { + MEDIA_ERR_LOG("HalCameraDeviceClose failed. ret(%d)", ret); + } + return CameraServiceCallback::CAMERA_STATUS_CLOSE; +} + +int32_t CameraService::SetCameraMode(uint8_t modeIndex) +{ + CodecDeinit(); + int32_t ret = HalCameraSetMode(modeIndex); + CodecInit(); + return ret; +} +} // namespace Media } // namespace OHOS \ No newline at end of file diff --git a/services/include/camera_server.h b/services/server/include/camera_server.h old mode 100755 new mode 100644 similarity index 97% rename from services/include/camera_server.h rename to services/server/include/camera_server.h index 8f5e399..b0fc60e --- a/services/include/camera_server.h +++ b/services/server/include/camera_server.h @@ -1,58 +1,58 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * 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. - */ -#ifndef CAMERA_SERVER_H -#define CAMERA_SERVER_H - -#include "camera_service.h" -#include "liteipc_adapter.h" -#include "serializer.h" -#include "camera_ability.h" - -namespace OHOS { -namespace Media { -class CameraServer { -public: - static CameraServer *GetInstance(); - ~CameraServer() {} - - static void CameraServerRequestHandle(int funcId, void *origin, IpcIo *req, IpcIo *reply); - void InitCameraServer(); - void GetCameraAbility(IpcIo *req, IpcIo *reply); - void GetCameraInfo(IpcIo *req, IpcIo *reply); - void GetCameraIdList(IpcIo *req, IpcIo *reply); - void CreateCamera(IpcIo *req, IpcIo *reply); - void CloseCamera(IpcIo *req, IpcIo *reply); - void SetCameraConfig(IpcIo *req, IpcIo *reply); - void SetFrameConfig(IpcIo *req, IpcIo *reply); - void TriggerLoopingCapture(IpcIo *req, IpcIo *reply); - void StopLoopingCapture(IpcIo *req, IpcIo *reply); - void TriggerSingleCapture(IpcIo *req, IpcIo *reply); - void SetCameraCallback(IpcIo *req, IpcIo *reply); - void OnTriggerSingleCaptureFinished(int32_t ret); - void OnTriggerLoopingCaptureFinished(int32_t ret, int32_t streamId); - void OnCameraStatusChange(int32_t ret, SvcIdentity *sid); - void OnCameraConfigured(int32_t ret); - void GetCameraModeNum(IpcIo *req, IpcIo *reply); - void SetCameraMode(IpcIo *req, IpcIo *reply); -private: - CameraServer() - { - sid_.cookie = 0; - } - SvcIdentity sid_; -}; -} // namespace Media -} // namespace OHOS -#endif // CAMERA_SERVER_H +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * 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. + */ +#ifndef CAMERA_SERVER_H +#define CAMERA_SERVER_H + +#include "camera_service.h" +#include "liteipc_adapter.h" +#include "serializer.h" +#include "camera_ability.h" + +namespace OHOS { +namespace Media { +class CameraServer { +public: + static CameraServer *GetInstance(); + ~CameraServer() {} + + static void CameraServerRequestHandle(int funcId, void *origin, IpcIo *req, IpcIo *reply); + void InitCameraServer(); + void GetCameraAbility(IpcIo *req, IpcIo *reply); + void GetCameraInfo(IpcIo *req, IpcIo *reply); + void GetCameraIdList(IpcIo *req, IpcIo *reply); + void CreateCamera(IpcIo *req, IpcIo *reply); + void CloseCamera(IpcIo *req, IpcIo *reply); + void SetCameraConfig(IpcIo *req, IpcIo *reply); + void SetFrameConfig(IpcIo *req, IpcIo *reply); + void TriggerLoopingCapture(IpcIo *req, IpcIo *reply); + void StopLoopingCapture(IpcIo *req, IpcIo *reply); + void TriggerSingleCapture(IpcIo *req, IpcIo *reply); + void SetCameraCallback(IpcIo *req, IpcIo *reply); + void OnTriggerSingleCaptureFinished(int32_t ret); + void OnTriggerLoopingCaptureFinished(int32_t ret, int32_t streamId); + void OnCameraStatusChange(int32_t ret, SvcIdentity *sid); + void OnCameraConfigured(int32_t ret); + void GetCameraModeNum(IpcIo *req, IpcIo *reply); + void SetCameraMode(IpcIo *req, IpcIo *reply); +private: + CameraServer() + { + sid_.cookie = 0; + } + SvcIdentity sid_; +}; +} // namespace Media +} // namespace OHOS +#endif // CAMERA_SERVER_H diff --git a/services/include/camera_type.h b/services/server/include/camera_type.h old mode 100755 new mode 100644 similarity index 96% rename from services/include/camera_type.h rename to services/server/include/camera_type.h index 4a78a5e..0ec848b --- a/services/include/camera_type.h +++ b/services/server/include/camera_type.h @@ -1,56 +1,56 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * 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. - */ - -#ifndef CAMERA_TYPE_H -#define CAMERA_TYPE_H - -#include - -namespace OHOS { -namespace Media { -typedef enum { - CAMERA_SERVER_GET_CAMERA_ABILITY, - CAMERA_SERVER_GET_CAMERA_INFO, - CAMERA_SERVER_GET_CAMERAIDLIST, - CAMERA_SERVER_CREATE_CAMERA, - CAMERA_SERVER_CLOSE_CAMERA, - CAEMRA_SERVER_SET_CAMERA_CONFIG, - CAMERA_SERVER_TRIGGER_SINGLE_CAPTURE, - CAMERA_SERVER_TRIGGER_LOOPING_CAPTURE, - CAMERA_SERVER_STOP_LOOPING_CAPTURE, - CAMERA_SERVER_SET_CAMERA_CALLBACK, - CAMERA_SERVER_GET_CAMERA_MODE_NUM, - CAMERA_SERVER_SET_CAMERA_MODE_NUM, -} CameraServerCall; - -typedef enum { - ON_CAMERA_STATUS_CHANGE, - ON_CAMERA_CONFIGURED, - ON_TRIGGER_SINGLE_CAPTURE_FINISHED, - ON_TRIGGER_LOOPING_CAPTURE_FINISHED -} CameraServerback; - -struct CallBackPara { - int funcId; - std::string cameraId; - void* data; - void* frameConfig; - void* cameraConfig; -}; -const char SERVICE_NAME[] = "CameraServer"; -static const int32_t DEFAULT_IPC_SIZE = 300; -} -} -#endif +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef CAMERA_TYPE_H +#define CAMERA_TYPE_H + +#include + +namespace OHOS { +namespace Media { +typedef enum { + CAMERA_SERVER_GET_CAMERA_ABILITY, + CAMERA_SERVER_GET_CAMERA_INFO, + CAMERA_SERVER_GET_CAMERAIDLIST, + CAMERA_SERVER_CREATE_CAMERA, + CAMERA_SERVER_CLOSE_CAMERA, + CAEMRA_SERVER_SET_CAMERA_CONFIG, + CAMERA_SERVER_TRIGGER_SINGLE_CAPTURE, + CAMERA_SERVER_TRIGGER_LOOPING_CAPTURE, + CAMERA_SERVER_STOP_LOOPING_CAPTURE, + CAMERA_SERVER_SET_CAMERA_CALLBACK, + CAMERA_SERVER_GET_CAMERA_MODE_NUM, + CAMERA_SERVER_SET_CAMERA_MODE_NUM, +} CameraServerCall; + +typedef enum { + ON_CAMERA_STATUS_CHANGE, + ON_CAMERA_CONFIGURED, + ON_TRIGGER_SINGLE_CAPTURE_FINISHED, + ON_TRIGGER_LOOPING_CAPTURE_FINISHED +} CameraServerback; + +struct CallBackPara { + int funcId; + std::string cameraId; + void* data; + void* frameConfig; + void* cameraConfig; +}; +const char SERVICE_NAME[] = "CameraServer"; +static const int32_t DEFAULT_IPC_SIZE = 300; +} +} +#endif diff --git a/services/src/camera_server.cpp b/services/server/src/camera_server.cpp old mode 100755 new mode 100644 similarity index 97% rename from services/src/camera_server.cpp rename to services/server/src/camera_server.cpp index 5666fef..3bd42a0 --- a/services/src/camera_server.cpp +++ b/services/server/src/camera_server.cpp @@ -1,368 +1,368 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * 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. - */ - -#include "camera_server.h" -#include -#include -#include -#include "camera_device.h" -#include "camera_service.h" -#include "camera_type.h" -#include "media_log.h" -#include "meta_data.h" -#include "surface.h" -#include "surface_impl.h" - -using namespace std; -namespace OHOS { -namespace Media { -CameraServer *CameraServer::GetInstance() -{ - static CameraServer server; - return &server; -} - -void CameraServer::CameraServerRequestHandle(int funcId, void *origin, IpcIo *req, IpcIo *reply) -{ - switch (funcId) { - case CAMERA_SERVER_GET_CAMERA_ABILITY: - CameraServer::GetInstance()->GetCameraAbility(req, reply); - break; - case CAMERA_SERVER_GET_CAMERA_INFO: - CameraServer::GetInstance()->GetCameraInfo(req, reply); - break; - case CAMERA_SERVER_GET_CAMERAIDLIST: - CameraServer::GetInstance()->GetCameraIdList(req, reply); - break; - case CAMERA_SERVER_CREATE_CAMERA: - CameraServer::GetInstance()->CreateCamera(req, reply); - break; - case CAMERA_SERVER_CLOSE_CAMERA: - CameraServer::GetInstance()->CloseCamera(req, reply); - break; - case CAEMRA_SERVER_SET_CAMERA_CONFIG: - CameraServer::GetInstance()->SetCameraConfig(req, reply); - break; - case CAMERA_SERVER_TRIGGER_LOOPING_CAPTURE: - CameraServer::GetInstance()->TriggerLoopingCapture(req, reply); - break; - case CAMERA_SERVER_STOP_LOOPING_CAPTURE: - CameraServer::GetInstance()->StopLoopingCapture(req, reply); - break; - case CAMERA_SERVER_TRIGGER_SINGLE_CAPTURE: - CameraServer::GetInstance()->TriggerSingleCapture(req, reply); - break; - case CAMERA_SERVER_SET_CAMERA_CALLBACK: - CameraServer::GetInstance()->SetCameraCallback(req, reply); - break; - case CAMERA_SERVER_SET_CAMERA_MODE_NUM: - CameraServer::GetInstance()->SetCameraMode(req, reply); - break; - case CAMERA_SERVER_GET_CAMERA_MODE_NUM: - CameraServer::GetInstance()->GetCameraModeNum(req, reply); - break; - default: - MEDIA_ERR_LOG("code not support:%d!", funcId); - break; - } -} -void CameraServer::InitCameraServer() -{ - CameraService *service = CameraService::GetInstance(); - service->Initialize(); -} - -void CameraServer::GetCameraAbility(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char*)(IpcIoPopString(req, &sz))); - CameraAbility *ability = CameraService::GetInstance()->GetCameraAbility(cameraId); - if (ability == nullptr) { - return; - } - list supportSizeList = ability->GetSupportedSizes(CAM_FORMAT_YVU420); - uint32_t supportProperties = 0; - IpcIoPushUint32(reply, supportProperties); - uint32_t listSize = supportSizeList.size(); - IpcIoPushUint32(reply, listSize); - for (auto supportSizeItem : supportSizeList) { - IpcIoPushFlatObj(reply, &supportSizeItem, sizeof(CameraPicSize)); - } - // af - list afModeList = ability->GetSupportedAfModes(); - uint32_t afListSize = afModeList.size(); - IpcIoPushUint32(reply, afListSize); - for (auto supportAfMode : afModeList) { - IpcIoPushInt32(reply, supportAfMode); - } - // ae - list aeModeList = ability->GetSupportedAeModes(); - uint32_t aeListSize = aeModeList.size(); - IpcIoPushUint32(reply, aeListSize); - for (auto supportAeMode : aeModeList) { - IpcIoPushInt32(reply, supportAeMode); - } -} - -void CameraServer::GetCameraInfo(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char*)(IpcIoPopString(req, &sz))); - CameraInfo *info = CameraService::GetInstance()->GetCameraInfo(cameraId); - if (info == nullptr) { - return; - } - IpcIoPushInt32(reply, info->GetCameraType()); - IpcIoPushInt32(reply, info->GetCameraFacingType()); -} - -void CameraServer::GetCameraIdList(IpcIo *req, IpcIo *reply) -{ - list cameraIdList = CameraService::GetInstance()->GetCameraIdList(); - IpcIoPushUint32(reply, cameraIdList.size()); - for (string cameraId : cameraIdList) { - IpcIoPushString(reply, cameraId.c_str()); - } -} - -void CameraServer::GetCameraModeNum(IpcIo *req, IpcIo *reply) -{ - uint8_t num = CameraService::GetInstance()->GetCameraModeNum(); - IpcIoPushUint8(reply, num); -} - -void CameraServer::CreateCamera(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char*)(IpcIoPopString(req, &sz))); - int32_t cameraStatus = CameraService::GetInstance()->CreateCamera(cameraId); - SvcIdentity *sid = IpcIoPopSvc(req); - if (sid == nullptr) { - MEDIA_ERR_LOG("sid is null, failed."); - return; - } -#ifdef __LINUX__ - BinderAcquire(sid->ipcContext, sid->handle); -#endif - OnCameraStatusChange(cameraStatus, sid); -} - -void CameraServer::CloseCamera(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char*)(IpcIoPopString(req, &sz))); - int32_t cameraStatus = CameraService::GetInstance()->CloseCamera(cameraId); - SvcIdentity *sid = IpcIoPopSvc(req); - if (sid == nullptr) { - MEDIA_ERR_LOG("sid is null, failed."); - return; - } -#ifdef __LINUX__ - BinderAcquire(sid->ipcContext, sid->handle); -#endif - OnCameraStatusChange(cameraStatus, sid); -} - -void CameraServer::SetCameraConfig(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char*)(IpcIoPopString(req, &sz))); - CameraDevice *device_ = CameraService::GetInstance()->GetCameraDevice(cameraId); - int32_t setStatus = device_->SetCameraConfig(); - IpcIoPushInt32(reply, setStatus); - OnCameraConfigured(setStatus); -} - -void CameraServer::SetCameraCallback(IpcIo *req, IpcIo *reply) -{ - SvcIdentity *sid = IpcIoPopSvc(req); - if (sid == nullptr) { - MEDIA_ERR_LOG("sid is null, failed."); - return; - } - sid_ = *sid; -#ifdef __LINUX__ - BinderAcquire(sid_.ipcContext, sid_.handle); -#endif -} - -FrameConfig *DeserializeFrameConfig(IpcIo &io) -{ - int32_t type = IpcIoPopInt32(&io); - auto fc = new FrameConfig(type); - - uint32_t surfaceNum = IpcIoPopUint32(&io); - for (uint32_t i = 0; i < surfaceNum; i++) { - Surface *surface = SurfaceImpl::GenericSurfaceByIpcIo(io); - if (surface == nullptr) { - MEDIA_ERR_LOG("Camera server receive null surface."); - delete fc; - return nullptr; - } - fc->AddSurface(*surface); - } - - int32_t qfactor = IpcIoPopInt32(&io); - if (qfactor >= 0) { - fc->SetParameter(PARAM_KEY_IMAGE_ENCODE_QFACTOR, qfactor); - } - - int32_t streamFps = IpcIoPopInt32(&io); - fc->SetParameter(CAM_FRAME_FPS, streamFps); - - int32_t invertMode = IpcIoPopInt32(&io); - fc->SetParameter(CAM_IMAGE_INVERT_MODE, invertMode); - - CameraRect streamCrop; - streamCrop.x = IpcIoPopInt32(&io); - streamCrop.y = IpcIoPopInt32(&io); - streamCrop.w = IpcIoPopInt32(&io); - streamCrop.h = IpcIoPopInt32(&io); - - int32_t streamFormat = IpcIoPopInt32(&io); - fc->SetParameter(CAM_IMAGE_FORMAT, streamFormat); - MEDIA_INFO_LOG("streamFormat is %d", streamFormat); - - if (type != FRAME_CONFIG_RECORD) { - BuffPtr *dataBuff = IpcIoPopDataBuff(&io); - if (dataBuff == nullptr || dataBuff->buff == nullptr) { - MEDIA_ERR_LOG("dataBuff is nullptr."); - return fc; - } - fc->SetVendorParameter((uint8_t *)dataBuff->buff, dataBuff->buffSz); - FreeBuffer(nullptr, dataBuff->buff); - } - return fc; -} - -void CameraServer::SetFrameConfig(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char *)(IpcIoPopString(req, &sz))); - int32_t streamId = IpcIoPopInt32(req); - MEDIA_ERR_LOG("SetFrameConfig streamId(%d).", streamId); - FrameConfig *fc = DeserializeFrameConfig(*req); - if (fc == nullptr) { - MEDIA_ERR_LOG("Deserialize frame config failed."); - return; - } - delete fc; -} - - -void CameraServer::TriggerLoopingCapture(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char*)(IpcIoPopString(req, &sz))); - CameraDevice *device_ = CameraService::GetInstance()->GetCameraDevice(cameraId); - FrameConfig *fc = DeserializeFrameConfig(*req); - if (fc == nullptr) { - MEDIA_ERR_LOG("Deserialize frame config failed."); - return; - } - uint32_t streamId = 0; - int32_t loopingCaptureStatus = device_->TriggerLoopingCapture(*fc, &streamId); - OnTriggerLoopingCaptureFinished(loopingCaptureStatus, streamId); - delete fc; -} - -void CameraServer::TriggerSingleCapture(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char *)(IpcIoPopString(req, &sz))); - CameraDevice *device_ = CameraService::GetInstance()->GetCameraDevice(cameraId); - FrameConfig *fc = DeserializeFrameConfig(*req); - if (fc == nullptr) { - MEDIA_ERR_LOG("Deserialize frame config failed."); - return; - } - uint32_t streamId = 0; - int32_t singleCaptureStatus = device_->TriggerSingleCapture(*fc, &streamId); - OnTriggerSingleCaptureFinished(singleCaptureStatus); - delete fc; -} - -void CameraServer::StopLoopingCapture(IpcIo *req, IpcIo *reply) -{ - size_t sz; - string cameraId((const char *)(IpcIoPopString(req, &sz))); - CameraDevice *device_ = CameraService::GetInstance()->GetCameraDevice(cameraId); - if (device_ == nullptr) { - MEDIA_INFO_LOG("device_ is null in camera_server.cpp!"); - return; - } - device_->StopLoopingCapture(); -} - -void CameraServer::OnCameraStatusChange(int32_t ret, SvcIdentity *sid) -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - IpcIoPushInt32(&io, ret); - int32_t ans = Transact(nullptr, *sid, ON_CAMERA_STATUS_CHANGE, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr); - if (ans != LITEIPC_OK) { - MEDIA_ERR_LOG("Create camera callback : on camera status change failed."); - } -} - -void CameraServer::OnTriggerSingleCaptureFinished(int32_t ret) -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - IpcIoPushInt32(&io, ret); - int32_t ans = Transact(nullptr, sid_, ON_TRIGGER_SINGLE_CAPTURE_FINISHED, - &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr); - if (ans != LITEIPC_OK) { - MEDIA_ERR_LOG("Trigger single capture callback : on trigger single capture frame finished failed."); - return; - } -} - -void CameraServer::OnTriggerLoopingCaptureFinished(int32_t ret, int32_t streamId) -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - IpcIoPushInt32(&io, ret); - int32_t ans = Transact(nullptr, sid_, ON_TRIGGER_LOOPING_CAPTURE_FINISHED, - &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr); - if (ans != LITEIPC_OK) { - MEDIA_ERR_LOG("Trigger looping capture callback : on trigger looping capture finished failed."); - } -} - -void CameraServer::OnCameraConfigured(int32_t ret) -{ - IpcIo io; - uint8_t tmpData[DEFAULT_IPC_SIZE]; - IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); - IpcIoPushInt32(&io, ret); - int32_t ans = Transact(nullptr, sid_, ON_CAMERA_CONFIGURED, - &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr); - if (ans != LITEIPC_OK) { - MEDIA_ERR_LOG("Camera config callback : on trigger looping capture finished failed."); - } -} - -void CameraServer::SetCameraMode(IpcIo *req, IpcIo *reply) -{ - uint8_t modeIndex = IpcIoPopUint8(req); - int32_t cameraStatus = CameraService::GetInstance()->SetCameraMode(modeIndex); - IpcIoPushInt32(reply, cameraStatus); -} -} // namespace Media +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * 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. + */ + +#include "camera_server.h" +#include +#include +#include +#include "camera_device.h" +#include "camera_service.h" +#include "camera_type.h" +#include "media_log.h" +#include "meta_data.h" +#include "surface.h" +#include "surface_impl.h" + +using namespace std; +namespace OHOS { +namespace Media { +CameraServer *CameraServer::GetInstance() +{ + static CameraServer server; + return &server; +} + +void CameraServer::CameraServerRequestHandle(int funcId, void *origin, IpcIo *req, IpcIo *reply) +{ + switch (funcId) { + case CAMERA_SERVER_GET_CAMERA_ABILITY: + CameraServer::GetInstance()->GetCameraAbility(req, reply); + break; + case CAMERA_SERVER_GET_CAMERA_INFO: + CameraServer::GetInstance()->GetCameraInfo(req, reply); + break; + case CAMERA_SERVER_GET_CAMERAIDLIST: + CameraServer::GetInstance()->GetCameraIdList(req, reply); + break; + case CAMERA_SERVER_CREATE_CAMERA: + CameraServer::GetInstance()->CreateCamera(req, reply); + break; + case CAMERA_SERVER_CLOSE_CAMERA: + CameraServer::GetInstance()->CloseCamera(req, reply); + break; + case CAEMRA_SERVER_SET_CAMERA_CONFIG: + CameraServer::GetInstance()->SetCameraConfig(req, reply); + break; + case CAMERA_SERVER_TRIGGER_LOOPING_CAPTURE: + CameraServer::GetInstance()->TriggerLoopingCapture(req, reply); + break; + case CAMERA_SERVER_STOP_LOOPING_CAPTURE: + CameraServer::GetInstance()->StopLoopingCapture(req, reply); + break; + case CAMERA_SERVER_TRIGGER_SINGLE_CAPTURE: + CameraServer::GetInstance()->TriggerSingleCapture(req, reply); + break; + case CAMERA_SERVER_SET_CAMERA_CALLBACK: + CameraServer::GetInstance()->SetCameraCallback(req, reply); + break; + case CAMERA_SERVER_SET_CAMERA_MODE_NUM: + CameraServer::GetInstance()->SetCameraMode(req, reply); + break; + case CAMERA_SERVER_GET_CAMERA_MODE_NUM: + CameraServer::GetInstance()->GetCameraModeNum(req, reply); + break; + default: + MEDIA_ERR_LOG("code not support:%d!", funcId); + break; + } +} +void CameraServer::InitCameraServer() +{ + CameraService *service = CameraService::GetInstance(); + service->Initialize(); +} + +void CameraServer::GetCameraAbility(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char*)(IpcIoPopString(req, &sz))); + CameraAbility *ability = CameraService::GetInstance()->GetCameraAbility(cameraId); + if (ability == nullptr) { + return; + } + list supportSizeList = ability->GetSupportedSizes(CAM_FORMAT_YVU420); + uint32_t supportProperties = 0; + IpcIoPushUint32(reply, supportProperties); + uint32_t listSize = supportSizeList.size(); + IpcIoPushUint32(reply, listSize); + for (auto supportSizeItem : supportSizeList) { + IpcIoPushFlatObj(reply, &supportSizeItem, sizeof(CameraPicSize)); + } + // af + list afModeList = ability->GetSupportedAfModes(); + uint32_t afListSize = afModeList.size(); + IpcIoPushUint32(reply, afListSize); + for (auto supportAfMode : afModeList) { + IpcIoPushInt32(reply, supportAfMode); + } + // ae + list aeModeList = ability->GetSupportedAeModes(); + uint32_t aeListSize = aeModeList.size(); + IpcIoPushUint32(reply, aeListSize); + for (auto supportAeMode : aeModeList) { + IpcIoPushInt32(reply, supportAeMode); + } +} + +void CameraServer::GetCameraInfo(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char*)(IpcIoPopString(req, &sz))); + CameraInfo *info = CameraService::GetInstance()->GetCameraInfo(cameraId); + if (info == nullptr) { + return; + } + IpcIoPushInt32(reply, info->GetCameraType()); + IpcIoPushInt32(reply, info->GetCameraFacingType()); +} + +void CameraServer::GetCameraIdList(IpcIo *req, IpcIo *reply) +{ + list cameraIdList = CameraService::GetInstance()->GetCameraIdList(); + IpcIoPushUint32(reply, cameraIdList.size()); + for (string cameraId : cameraIdList) { + IpcIoPushString(reply, cameraId.c_str()); + } +} + +void CameraServer::GetCameraModeNum(IpcIo *req, IpcIo *reply) +{ + uint8_t num = CameraService::GetInstance()->GetCameraModeNum(); + IpcIoPushUint8(reply, num); +} + +void CameraServer::CreateCamera(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char*)(IpcIoPopString(req, &sz))); + int32_t cameraStatus = CameraService::GetInstance()->CreateCamera(cameraId); + SvcIdentity *sid = IpcIoPopSvc(req); + if (sid == nullptr) { + MEDIA_ERR_LOG("sid is null, failed."); + return; + } +#ifdef __LINUX__ + BinderAcquire(sid->ipcContext, sid->handle); +#endif + OnCameraStatusChange(cameraStatus, sid); +} + +void CameraServer::CloseCamera(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char*)(IpcIoPopString(req, &sz))); + int32_t cameraStatus = CameraService::GetInstance()->CloseCamera(cameraId); + SvcIdentity *sid = IpcIoPopSvc(req); + if (sid == nullptr) { + MEDIA_ERR_LOG("sid is null, failed."); + return; + } +#ifdef __LINUX__ + BinderAcquire(sid->ipcContext, sid->handle); +#endif + OnCameraStatusChange(cameraStatus, sid); +} + +void CameraServer::SetCameraConfig(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char*)(IpcIoPopString(req, &sz))); + CameraDevice *device_ = CameraService::GetInstance()->GetCameraDevice(cameraId); + int32_t setStatus = device_->SetCameraConfig(); + IpcIoPushInt32(reply, setStatus); + OnCameraConfigured(setStatus); +} + +void CameraServer::SetCameraCallback(IpcIo *req, IpcIo *reply) +{ + SvcIdentity *sid = IpcIoPopSvc(req); + if (sid == nullptr) { + MEDIA_ERR_LOG("sid is null, failed."); + return; + } + sid_ = *sid; +#ifdef __LINUX__ + BinderAcquire(sid_.ipcContext, sid_.handle); +#endif +} + +FrameConfig *DeserializeFrameConfig(IpcIo &io) +{ + int32_t type = IpcIoPopInt32(&io); + auto fc = new FrameConfig(type); + + uint32_t surfaceNum = IpcIoPopUint32(&io); + for (uint32_t i = 0; i < surfaceNum; i++) { + Surface *surface = SurfaceImpl::GenericSurfaceByIpcIo(io); + if (surface == nullptr) { + MEDIA_ERR_LOG("Camera server receive null surface."); + delete fc; + return nullptr; + } + fc->AddSurface(*surface); + } + + int32_t qfactor = IpcIoPopInt32(&io); + if (qfactor >= 0) { + fc->SetParameter(PARAM_KEY_IMAGE_ENCODE_QFACTOR, qfactor); + } + + int32_t streamFps = IpcIoPopInt32(&io); + fc->SetParameter(CAM_FRAME_FPS, streamFps); + + int32_t invertMode = IpcIoPopInt32(&io); + fc->SetParameter(CAM_IMAGE_INVERT_MODE, invertMode); + + CameraRect streamCrop; + streamCrop.x = IpcIoPopInt32(&io); + streamCrop.y = IpcIoPopInt32(&io); + streamCrop.w = IpcIoPopInt32(&io); + streamCrop.h = IpcIoPopInt32(&io); + + int32_t streamFormat = IpcIoPopInt32(&io); + fc->SetParameter(CAM_IMAGE_FORMAT, streamFormat); + MEDIA_INFO_LOG("streamFormat is %d", streamFormat); + + if (type != FRAME_CONFIG_RECORD) { + BuffPtr *dataBuff = IpcIoPopDataBuff(&io); + if (dataBuff == nullptr || dataBuff->buff == nullptr) { + MEDIA_ERR_LOG("dataBuff is nullptr."); + return fc; + } + fc->SetVendorParameter((uint8_t *)dataBuff->buff, dataBuff->buffSz); + FreeBuffer(nullptr, dataBuff->buff); + } + return fc; +} + +void CameraServer::SetFrameConfig(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char *)(IpcIoPopString(req, &sz))); + int32_t streamId = IpcIoPopInt32(req); + MEDIA_ERR_LOG("SetFrameConfig streamId(%d).", streamId); + FrameConfig *fc = DeserializeFrameConfig(*req); + if (fc == nullptr) { + MEDIA_ERR_LOG("Deserialize frame config failed."); + return; + } + delete fc; +} + + +void CameraServer::TriggerLoopingCapture(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char*)(IpcIoPopString(req, &sz))); + CameraDevice *device_ = CameraService::GetInstance()->GetCameraDevice(cameraId); + FrameConfig *fc = DeserializeFrameConfig(*req); + if (fc == nullptr) { + MEDIA_ERR_LOG("Deserialize frame config failed."); + return; + } + uint32_t streamId = 0; + int32_t loopingCaptureStatus = device_->TriggerLoopingCapture(*fc, &streamId); + OnTriggerLoopingCaptureFinished(loopingCaptureStatus, streamId); + delete fc; +} + +void CameraServer::TriggerSingleCapture(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char *)(IpcIoPopString(req, &sz))); + CameraDevice *device_ = CameraService::GetInstance()->GetCameraDevice(cameraId); + FrameConfig *fc = DeserializeFrameConfig(*req); + if (fc == nullptr) { + MEDIA_ERR_LOG("Deserialize frame config failed."); + return; + } + uint32_t streamId = 0; + int32_t singleCaptureStatus = device_->TriggerSingleCapture(*fc, &streamId); + OnTriggerSingleCaptureFinished(singleCaptureStatus); + delete fc; +} + +void CameraServer::StopLoopingCapture(IpcIo *req, IpcIo *reply) +{ + size_t sz; + string cameraId((const char *)(IpcIoPopString(req, &sz))); + CameraDevice *device_ = CameraService::GetInstance()->GetCameraDevice(cameraId); + if (device_ == nullptr) { + MEDIA_INFO_LOG("device_ is null in camera_server.cpp!"); + return; + } + device_->StopLoopingCapture(); +} + +void CameraServer::OnCameraStatusChange(int32_t ret, SvcIdentity *sid) +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + IpcIoPushInt32(&io, ret); + int32_t ans = Transact(nullptr, *sid, ON_CAMERA_STATUS_CHANGE, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr); + if (ans != LITEIPC_OK) { + MEDIA_ERR_LOG("Create camera callback : on camera status change failed."); + } +} + +void CameraServer::OnTriggerSingleCaptureFinished(int32_t ret) +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + IpcIoPushInt32(&io, ret); + int32_t ans = Transact(nullptr, sid_, ON_TRIGGER_SINGLE_CAPTURE_FINISHED, + &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr); + if (ans != LITEIPC_OK) { + MEDIA_ERR_LOG("Trigger single capture callback : on trigger single capture frame finished failed."); + return; + } +} + +void CameraServer::OnTriggerLoopingCaptureFinished(int32_t ret, int32_t streamId) +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + IpcIoPushInt32(&io, ret); + int32_t ans = Transact(nullptr, sid_, ON_TRIGGER_LOOPING_CAPTURE_FINISHED, + &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr); + if (ans != LITEIPC_OK) { + MEDIA_ERR_LOG("Trigger looping capture callback : on trigger looping capture finished failed."); + } +} + +void CameraServer::OnCameraConfigured(int32_t ret) +{ + IpcIo io; + uint8_t tmpData[DEFAULT_IPC_SIZE]; + IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0); + IpcIoPushInt32(&io, ret); + int32_t ans = Transact(nullptr, sid_, ON_CAMERA_CONFIGURED, + &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr); + if (ans != LITEIPC_OK) { + MEDIA_ERR_LOG("Camera config callback : on trigger looping capture finished failed."); + } +} + +void CameraServer::SetCameraMode(IpcIo *req, IpcIo *reply) +{ + uint8_t modeIndex = IpcIoPopUint8(req); + int32_t cameraStatus = CameraService::GetInstance()->SetCameraMode(modeIndex); + IpcIoPushInt32(reply, cameraStatus); +} +} // namespace Media } // namespace OHOS \ No newline at end of file diff --git a/services/src/samgr_camera.cpp b/services/server/src/samgr_camera.cpp old mode 100755 new mode 100644 similarity index 96% rename from services/src/samgr_camera.cpp rename to services/server/src/samgr_camera.cpp index da2ae59..4ef2851 --- a/services/src/samgr_camera.cpp +++ b/services/server/src/samgr_camera.cpp @@ -1,94 +1,94 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * 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. - */ - -#include -#include -#include - -#include "feature.h" -#include "media_log.h" -#include "iproxy_client.h" -#include "iproxy_server.h" -#include "iunknown.h" -#include "samgr_lite.h" -#include "service.h" - -#include "camera_type.h" -#include "camera_server.h" - -namespace OHOS { -struct DefaultFeatureApi { - INHERIT_SERVER_IPROXY; -}; - -struct CameraService { - INHERIT_SERVICE; - INHERIT_IUNKNOWNENTRY(DefaultFeatureApi); - Identity identity; -}; - -static const char* GetName(Service* service) -{ - (void)service; - return Media::SERVICE_NAME; -} - -static BOOL Initialize(Service* service, Identity identity) -{ - CameraService* example = (CameraService*)service; - example->identity = identity; - MEDIA_INFO_LOG("Initialize(%s)! Identity<%d, %d, %p>", Media::SERVICE_NAME, - identity.serviceId, identity.featureId, identity.queueId); - return TRUE; -} - -static BOOL MessageHandle(Service* service, Request* msg) -{ - MEDIA_INFO_LOG("MessageHandle(%s)! Request<%d, %d, %p>", - service->GetName(service), msg->msgId, msg->msgValue, msg->data); - return FALSE; -} - -static TaskConfig GetTaskConfig(Service* service) -{ - (void)service; - TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, 0x800, 20, SHARED_TASK}; - return config; -} - -static int32 Invoke(IServerProxy* iProxy, int funcId, void* origin, IpcIo* req, IpcIo* reply) -{ - Media::CameraServer::CameraServerRequestHandle(funcId, origin, req, reply); - return EC_SUCCESS; -} - -static CameraService g_example = { - .GetName = GetName, - .Initialize = Initialize, - .MessageHandle = MessageHandle, - .GetTaskConfig = GetTaskConfig, - SERVER_IPROXY_IMPL_BEGIN, - .Invoke = Invoke, - IPROXY_END, -}; - -static void Init(void) -{ - SAMGR_GetInstance()->RegisterService((Service*)&g_example); - SAMGR_GetInstance()->RegisterDefaultFeatureApi(Media::SERVICE_NAME, GET_IUNKNOWN(g_example)); -} - -SYSEX_SERVICE_INIT(Init); +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * 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. + */ + +#include +#include +#include + +#include "feature.h" +#include "media_log.h" +#include "iproxy_client.h" +#include "iproxy_server.h" +#include "iunknown.h" +#include "samgr_lite.h" +#include "service.h" + +#include "camera_type.h" +#include "camera_server.h" + +namespace OHOS { +struct DefaultFeatureApi { + INHERIT_SERVER_IPROXY; +}; + +struct CameraService { + INHERIT_SERVICE; + INHERIT_IUNKNOWNENTRY(DefaultFeatureApi); + Identity identity; +}; + +static const char* GetName(Service* service) +{ + (void)service; + return Media::SERVICE_NAME; +} + +static BOOL Initialize(Service* service, Identity identity) +{ + CameraService* example = (CameraService*)service; + example->identity = identity; + MEDIA_INFO_LOG("Initialize(%s)! Identity<%d, %d, %p>", Media::SERVICE_NAME, + identity.serviceId, identity.featureId, identity.queueId); + return TRUE; +} + +static BOOL MessageHandle(Service* service, Request* msg) +{ + MEDIA_INFO_LOG("MessageHandle(%s)! Request<%d, %d, %p>", + service->GetName(service), msg->msgId, msg->msgValue, msg->data); + return FALSE; +} + +static TaskConfig GetTaskConfig(Service* service) +{ + (void)service; + TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, 0x800, 20, SHARED_TASK}; + return config; +} + +static int32 Invoke(IServerProxy* iProxy, int funcId, void* origin, IpcIo* req, IpcIo* reply) +{ + Media::CameraServer::CameraServerRequestHandle(funcId, origin, req, reply); + return EC_SUCCESS; +} + +static CameraService g_example = { + .GetName = GetName, + .Initialize = Initialize, + .MessageHandle = MessageHandle, + .GetTaskConfig = GetTaskConfig, + SERVER_IPROXY_IMPL_BEGIN, + .Invoke = Invoke, + IPROXY_END, +}; + +static void Init(void) +{ + SAMGR_GetInstance()->RegisterService((Service*)&g_example); + SAMGR_GetInstance()->RegisterDefaultFeatureApi(Media::SERVICE_NAME, GET_IUNKNOWN(g_example)); +} + +SYSEX_SERVICE_INIT(Init); } // namespace OHOS \ No newline at end of file -- Gitee