1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_CAMERA_DPS_VIDEO_POST_PROCESSOR_H
17 #define OHOS_CAMERA_DPS_VIDEO_POST_PROCESSOR_H
18 
19 #include "basic_definitions.h"
20 #include "deferred_video_job.h"
21 #include "iservstat_listener_hdi.h"
22 #include "mpeg_manager.h"
23 #include "safe_map.h"
24 #include "v1_3/ivideo_process_service.h"
25 #include "v1_3/ivideo_process_session.h"
26 #include "v1_3/ivideo_process_callback.h"
27 
28 namespace OHOS {
29 namespace CameraStandard {
30 namespace DeferredProcessing {
31 using OHOS::HDI::Camera::V1_3::IVideoProcessSession;
32 using OHOS::HDI::Camera::V1_3::StreamDescription;
33 using OHOS::HDI::Camera::V1_1::StreamInfo_V1_1;
34 using HDI::Camera::V1_0::BufferProducerSequenceable;
35 
36 class VideoPostProcessor : public std::enable_shared_from_this<VideoPostProcessor> {
37 public:
38     ~VideoPostProcessor();
39 
40     void Initialize();
41     bool GetPendingVideos(std::vector<std::string>& pendingVideos);
42     void SetExecutionMode(ExecutionMode executionMode);
43     void SetDefaultExecutionMode();
44     void ProcessRequest(const DeferredVideoWorkPtr& work);
45     void RemoveRequest(const std::string& videoId);
46     void PauseRequest(const std::string& videoId, const ScheduleType& type);
47 
48 protected:
49     explicit VideoPostProcessor(const int32_t userId);
50 
51 private:
52     class VideoServiceListener;
53     class SessionDeathRecipient;
54     class VideoProcessListener;
55 
56     void ConnectService();
57     void DisconnectService();
58     bool PrepareStreams(const std::string& videoId, const int inputFd);
59     void CreateSurface(const std::string& name, const StreamDescription& stream, sptr<Surface>& surface);
60     void SetStreamInfo(const StreamDescription& stream, sptr<BufferProducerSequenceable>& producer);
61     bool StartMpeg(const std::string& videoId, const sptr<IPCFileDescriptor>& inputFd);
62     bool StopMpeg(const MediaResult result, const DeferredVideoWorkPtr& work);
63     void ReleaseMpeg();
64     void StartTimer(const std::string& videoId, const DeferredVideoWorkPtr& work);
65     void StopTimer(const std::string& videoId);
66     void OnTimerOut(const std::string& videoId);
67     void OnServiceChange(const HDI::ServiceManager::V1_0::ServiceStatus& status);
68     void OnSessionDied();
69     void OnProcessDone(const std::string& videoId);
70     void OnError(const std::string& videoId, DpsError errorCode);
71     void OnStateChanged(HdiStatus hdiStatus);
72     DeferredVideoWorkPtr GetRunningWork(const std::string& videoId);
73     void copyFileByFd(const int srcFd, const int dstFd);
74     DpsError MapHdiError(OHOS::HDI::Camera::V1_2::ErrorCode errorCode);
75 
GetVideoSession()76     inline sptr<IVideoProcessSession> GetVideoSession()
77     {
78         std::lock_guard<std::mutex> lock(sessionMutex_);
79         return session_;
80     }
81 
SetVideoSession(const sptr<IVideoProcessSession> & session)82     inline void SetVideoSession(const sptr<IVideoProcessSession>& session)
83     {
84         std::lock_guard<std::mutex> lock(sessionMutex_);
85         session_ = session;
86     }
87 
GetMpegManager()88     inline std::shared_ptr<MpegManager> GetMpegManager()
89     {
90         std::lock_guard<std::mutex> lock(mpegManagerMutex_);
91         return mpegManager_;
92     }
93 
SetMpegManager(const std::shared_ptr<MpegManager> & mpegManager)94     inline void SetMpegManager(const std::shared_ptr<MpegManager>& mpegManager)
95     {
96         std::lock_guard<std::mutex> lock(mpegManagerMutex_);
97         mpegManager_ = mpegManager;
98         return;
99     }
100 
101     std::mutex mpegManagerMutex_;
102     std::mutex sessionMutex_;
103     const int32_t userId_;
104     int32_t timeoutCount_ {0};
105     sptr<IVideoProcessSession> session_ {nullptr};
106     std::shared_ptr<MpegManager> mpegManager_ {nullptr};
107     sptr<VideoServiceListener> serviceListener_;
108     sptr<SessionDeathRecipient> sessionDeathRecipient_;
109     sptr<VideoProcessListener> processListener_;
110     std::vector<StreamInfo_V1_1> allStreamInfo_ {};
111     SafeMap<std::string, DeferredVideoWorkPtr> videoId2Handle_ {};
112 };
113 } // namespace DeferredProcessing
114 } // namespace CameraStandard
115 } // namespace OHOS
116 #endif // OHOS_CAMERA_DPS_VIDEO_POST_PROCESSOR_H