/* * Copyright (c) 2021-2022 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_H_CAPTURE_SESSION_H #define OHOS_CAMERA_H_CAPTURE_SESSION_H #define EXPORT_API __attribute__((visibility("default"))) #include #include #include #include #include #include #include #include #include #include #include "camera_util.h" #include "camera_dynamic_loader.h" #include "fixed_size_list.h" #include "hcamera_device.h" #include "hcapture_session_stub.h" #include "hstream_capture.h" #include "hstream_metadata.h" #include "hstream_repeat.h" #include "icapture_session.h" #include "istream_common.h" #include "camera_photo_proxy.h" #include "moving_photo/moving_photo_surface_wrapper.h" #include "surface.h" #include "v1_0/istream_operator.h" #include "v1_1/istream_operator.h" #include "v1_2/istream_operator.h" #include "v1_3/istream_operator_callback.h" #include "hcamera_restore_param.h" #include "iconsumer_surface.h" #include "blocking_queue.h" #include "audio_capturer.h" #include "audio_info.h" #include "avcodec_task_manager.h" #include "moving_photo_video_cache.h" #include "drain_manager.h" #include "audio_capturer_session.h" #include "safe_map.h" #ifdef CAMERA_USE_SENSOR #include "sensor_agent.h" #include "sensor_agent_type.h" #endif namespace OHOS::Media { class Picture; } namespace OHOS { namespace CameraStandard { using OHOS::HDI::Camera::V1_0::CaptureEndedInfo; using OHOS::HDI::Camera::V1_0::CaptureErrorInfo; using namespace AudioStandard; using namespace std::chrono; using namespace DeferredProcessing; using namespace Media; class PermissionStatusChangeCb; class CameraUseStateChangeCb; class DisplayRotationListener; class CameraServerPhotoProxy; static const int32_t STREAM_NOT_FOUNT = -1; enum class CaptureSessionReleaseType : int32_t { RELEASE_TYPE_CLIENT = 0, RELEASE_TYPE_CLIENT_DIED, RELEASE_TYPE_SECURE, RELEASE_TYPE_OBJ_DIED, }; class StateMachine { public: explicit StateMachine(); virtual ~StateMachine() = default; bool CheckTransfer(CaptureSessionState targetState); bool Transfer(CaptureSessionState targetState); inline CaptureSessionState GetCurrentState() { std::lock_guard lock(sessionStateLock_); return currentState_; } inline void StateGuard(const std::function& fun) { std::lock_guard lock(sessionStateLock_); fun(currentState_); } inline bool IsStateNoLock(CaptureSessionState targetState) { return currentState_ == targetState; } private: std::vector stateTransferMap_[static_cast(CaptureSessionState::SESSION_STATE_MAX)]; std::recursive_mutex sessionStateLock_; CaptureSessionState currentState_ = CaptureSessionState::SESSION_INIT; }; class StreamContainer { public: StreamContainer() {}; virtual ~StreamContainer() = default; bool AddStream(sptr stream); bool RemoveStream(sptr stream); sptr GetStream(int32_t streamId); sptr GetHdiStream(int32_t streamId); void Clear(); size_t Size(); std::list> GetStreams(const StreamType streamType); std::list> GetAllStreams(); private: std::mutex streamsLock_; std::map>> streams_; }; class StreamOperatorCallback : public OHOS::HDI::Camera::V1_3::IStreamOperatorCallback { public: StreamOperatorCallback() = default; virtual ~StreamOperatorCallback() = default; int32_t OnCaptureStarted(int32_t captureId, const std::vector& streamIds) override; int32_t OnCaptureStarted_V1_2( int32_t captureId, const std::vector& infos) override; int32_t OnCaptureEnded(int32_t captureId, const std::vector& infos) override; int32_t OnCaptureEndedExt( int32_t captureId, const std::vector& infos) override; int32_t OnCaptureError(int32_t captureId, const std::vector& infos) override; int32_t OnFrameShutter(int32_t captureId, const std::vector& streamIds, uint64_t timestamp) override; int32_t OnFrameShutterEnd(int32_t captureId, const std::vector& streamIds, uint64_t timestamp) override; int32_t OnCaptureReady(int32_t captureId, const std::vector& streamIds, uint64_t timestamp) override; int32_t OnResult(int32_t streamId, const std::vector& result) override; virtual const sptr GetStreamByStreamID(int32_t streamId) = 0; virtual const sptr GetHdiStreamByStreamID(int32_t streamId) = 0; virtual void StartMovingPhotoEncode(int32_t rotation, uint64_t timestamp, int32_t format, int32_t captureId) = 0; private: std::mutex cbMutex_; }; class SessionDrainImageCallback; using MetaElementType = std::pair>; class MovingPhotoListener : public MovingPhotoSurfaceWrapper::SurfaceBufferListener { public: explicit MovingPhotoListener(sptr surfaceWrapper, sptr metaSurface, shared_ptr> metaCache, uint32_t preCacheFrameCount, uint32_t postCacheFrameCount); ~MovingPhotoListener() override; void OnBufferArrival(sptr buffer, int64_t timestamp, GraphicTransformType transform) override; void DrainOutImage(sptr drainImageCallback); void RemoveDrainImageManager(sptr drainImageCallback); void StopDrainOut(); void ClearCache(uint64_t timestamp); void SetClearFlag(); private: sptr movingPhotoSurfaceWrapper_; sptr metaSurface_; shared_ptr> metaCache_; BlockingQueue> recorderBufferQueue_; SafeMap, sptr> callbackMap_; std::atomic isNeededClear_ { false }; std::atomic isNeededPop_ { false }; int64_t shutterTime_; uint64_t postCacheFrameCount_; }; class MovingPhotoMetaListener : public IBufferConsumerListener { public: explicit MovingPhotoMetaListener(sptr surface, shared_ptr> metaCache); ~MovingPhotoMetaListener(); void OnBufferAvailable() override; private: sptr surface_; shared_ptr> metaCache_; }; class SessionDrainImageCallback : public DrainImageCallback { public: explicit SessionDrainImageCallback(std::vector>& frameCacheList, wptr listener, wptr cache, uint64_t timestamp, int32_t rotation, int32_t captureId); ~SessionDrainImageCallback(); void OnDrainImage(sptr frame) override; void OnDrainImageFinish(bool isFinished) override; private: std::mutex mutex_; std::vector> frameCacheList_; wptr listener_; wptr videoCache_; uint64_t timestamp_; int32_t rotation_; int32_t captureId_; }; class CameraInfoDumper; class EXPORT_API HCaptureSession : public HCaptureSessionStub, public StreamOperatorCallback { public: static sptr NewInstance(const uint32_t callerToken, int32_t opMode); HCaptureSession(); explicit HCaptureSession(const uint32_t callingTokenId, int32_t opMode); virtual ~HCaptureSession(); int32_t BeginConfig() override; int32_t CommitConfig() override; int32_t CanAddInput(sptr cameraDevice, bool& result) override; int32_t AddInput(sptr cameraDevice) override; int32_t AddOutput(StreamType streamType, sptr stream) override; int32_t RemoveInput(sptr cameraDevice) override; int32_t RemoveOutput(StreamType streamType, sptr stream) override; int32_t Start() override; int32_t Stop() override; int32_t Release() override; int32_t Release(CaptureSessionReleaseType type); static void DestroyStubObjectForPid(pid_t pid); int32_t SetCallback(sptr& callback) override; int32_t GetSessionState(CaptureSessionState& sessionState) override; int32_t GetActiveColorSpace(ColorSpace& colorSpace) override; int32_t SetColorSpace(ColorSpace colorSpace, ColorSpace captureColorSpace, bool isNeedUpdate) override; bool QueryFpsAndZoomRatio(float& currentFps, float& currentZoomRatio); bool QueryZoomPerformance(std::vector& crossZoomAndTime, int32_t operationMode); int32_t SetSmoothZoom( int32_t smoothZoomType, int32_t operationMode, float targetZoomRatio, float& duration) override; int32_t EnableMovingPhoto(bool isEnable) override; pid_t GetPid(); int32_t GetCurrentStreamInfos(std::vector& streamInfos); int32_t GetopMode(); int32_t OperatePermissionCheck(uint32_t interfaceCode) override; int32_t EnableMovingPhotoMirror(bool isMirror, bool isConfig) override; int32_t CreateMediaLibrary(sptr& photoProxy, std::string& uri, int32_t& cameraShotType, std::string& burstKey, int64_t timestamp) override; int32_t CreateMediaLibrary(std::unique_ptr picture, sptr &photoProxy, std::string &uri, int32_t &cameraShotType, std::string& burstKey, int64_t timestamp) override; void SetCameraPhotoProxyInfo(sptr cameraPhotoProxy, int32_t &cameraShotType, bool &isBursting, std::string &burstKey); const sptr GetStreamByStreamID(int32_t streamId) override; int32_t SetFeatureMode(int32_t featureMode) override; const sptr GetHdiStreamByStreamID(int32_t streamId) override; void StartMovingPhotoEncode(int32_t rotation, uint64_t timestamp, int32_t format, int32_t captureId) override; void StartRecord(uint64_t timestamp, int32_t rotation, int32_t captureId); void GetOutputStatus(int32_t &status); int32_t SetPreviewRotation(std::string &deviceClass) override; void DumpSessionInfo(CameraInfoDumper& infoDumper); static void DumpSessions(CameraInfoDumper& infoDumper); static void DumpCameraSessionSummary(CameraInfoDumper& infoDumper); void ReleaseStreams(); void StopMovingPhoto(); static void OpenMediaLib(); static void DelayCloseMediaLib(); static std::optional closeTimerId_; static std::mutex g_mediaTaskLock_; std::atomic isNeedMediaLib = false; uint32_t preCacheFrameCount_ = CACHE_FRAME_COUNT; uint32_t postCacheFrameCount_ = CACHE_FRAME_COUNT; private: int32_t Initialize(const uint32_t callerToken, int32_t opMode); string lastDisplayName_ = ""; string lastBurstPrefix_ = ""; int32_t saveIndex = 0; volatile bool isMovingPhotoMirror_ = false; volatile bool isSetMotionPhoto_ = false; std::mutex livePhotoStreamLock_; // Guard livePhotoStreamRepeat_ sptr livePhotoStreamRepeat_; inline void SetCameraDevice(sptr device) { std::lock_guard lock(cameraDeviceLock_); cameraDevice_ = device; } inline const sptr GetCameraDevice() { std::lock_guard lock(cameraDeviceLock_); return cameraDevice_; } string CreateDisplayName(const std::string& suffix); string CreateBurstDisplayName(int32_t imageSeqId, int32_t seqId); int32_t ValidateSessionInputs(); int32_t ValidateSessionOutputs(); int32_t ValidateSession(); int32_t AddOutputStream(sptr stream); int32_t RemoveOutputStream(sptr stream); int32_t LinkInputAndOutputs(); int32_t UnlinkInputAndOutputs(); void ClearSketchRepeatStream(); void ExpandSketchRepeatStream(); void ExpandMovingPhotoRepeatStream(); void ClearMovingPhotoRepeatStream(); int32_t CreateMovingPhotoStreamRepeat(int32_t format, int32_t width, int32_t height, sptr producer); int32_t CheckIfColorSpaceMatchesFormat(ColorSpace colorSpace); void CancelStreamsAndGetStreamInfos(std::vector& streamInfos); void RestartStreams(); int32_t UpdateStreamInfos(); void SetColorSpaceForStreams(); void ProcessMetaZoomArray(std::vector& zoomAndTimeArray, sptr& cameraDevice); void StartMovingPhotoStream(); bool InitAudioCapture(); bool StartAudioCapture(); void ProcessAudioBuffer(); void StartOnceRecord(uint64_t timestamp, int32_t rotation, int32_t captureId); int32_t StartPreviewStream(const std::shared_ptr& settings, camera_position_enum_t cameraPosition); void UpdateMuteSetting(bool muteMode, std::shared_ptr &settings); int32_t GetSensorOritation(); int32_t GetMovingPhotoBufferDuration(); void GetMovingPhotoStartAndEndTime(); void StartMovingPhoto(sptr& curStreamRepeat); std::string GetSessionState(); void DynamicConfigStream(); bool IsNeedDynamicConfig(); void RegisterDisplayListener(sptr repeat); void UnRegisterDisplayListener(sptr repeat); StateMachine stateMachine_; #ifdef CAMERA_USE_SENSOR std::mutex sensorLock_; bool isRegisterSensorSuccess_ = false; void RegisterSensorCallback(); void UnRegisterSensorCallback(); static void GravityDataCallbackImpl(SensorEvent *event); static int32_t CalcSensorRotation(int32_t sensorDegree); static int32_t CalcRotationDegree(GravityData data); #endif // Make sure device thread safe,set device by {SetCameraDevice}, get device by {GetCameraDevice} std::mutex cameraDeviceLock_; sptr cameraDevice_; StreamContainer streamContainer_; #ifdef CAMERA_USE_SENSOR SensorUser user = {"", nullptr, nullptr}; #endif pid_t pid_; uid_t uid_; uint32_t callerToken_; int32_t opMode_; int32_t featureMode_; ColorSpace currColorSpace_ = ColorSpace::COLOR_SPACE_UNKNOWN; ColorSpace currCaptureColorSpace_ = ColorSpace::COLOR_SPACE_UNKNOWN; bool isSessionStarted_ = false; bool enableStreamRotate_ = false; bool isDynamicConfiged_ = false; std::string deviceClass_ = "phone"; std::mutex movingPhotoStatusLock_; // Guard movingPhotoStatus sptr livephotoListener_; sptr livephotoMetaListener_; sptr audioCapturerSession_; sptr metaSurface_ = nullptr; sptr videoCache_; sptr taskManager_; std::mutex displayListenerLock_; sptr displayListener_; }; } // namespace CameraStandard } // namespace OHOS #endif // OHOS_CAMERA_H_CAPTURE_SESSION_H