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_JOB_REPOSITORY_H
17 #define OHOS_CAMERA_DPS_VIDEO_JOB_REPOSITORY_H
18 
19 #include <unordered_set>
20 
21 #include "deferred_video_job.h"
22 #include "ivideo_job_repository_listener.h"
23 #include "video_job_queue.h"
24 
25 namespace OHOS {
26 namespace CameraStandard {
27 namespace DeferredProcessing {
28 class VideoJobRepository {
29 public:
30     VideoJobRepository(const int32_t userId);
31     ~VideoJobRepository();
32 
33     void AddVideoJob(const std::string& videoId, const sptr<IPCFileDescriptor>& srcFd,
34         const sptr<IPCFileDescriptor>& dstFd);
35     bool RemoveVideoJob(const std::string& videoId, bool restorable);
36     void RestoreVideoJob(const std::string& videoId);
37     void SetJobPending(const std::string& videoId);
38     void SetJobRunning(const std::string& videoId);
39     void SetJobCompleted(const std::string& videoId);
40     void SetJobFailed(const std::string& videoId);
41     void SetJobPause(const std::string& videoId);
42     void SetJobError(const std::string& videoId);
43 
44     DeferredVideoJobPtr GetJob();
45     int32_t GetRunningJobCounts();
46     void GetRunningJobList(std::vector<std::string>& list);
47     void RegisterJobListener(const std::weak_ptr<IVideoJobRepositoryListener>& listener);
48 
49 private:
50     void NotifyJobChangedUnLocked(bool statusChanged, DeferredVideoJobPtr jobPtr);
51     void UpdateRunningCountUnLocked(bool statusChanged, const DeferredVideoJobPtr& jobPtr);
52     DeferredVideoJobPtr GetJobUnLocked(const std::string& videoId);
53     void ClearCatch();
54 
55     std::recursive_mutex mutex_;
56     const int32_t userId_;
57     std::shared_ptr<VideoJobQueue> jobQueue_ {nullptr};
58     std::unordered_map<std::string, DeferredVideoJobPtr> jobMap_ {};
59     std::unordered_set<std::string> runningSet_ {};
60     std::weak_ptr<IVideoJobRepositoryListener> jobListener_ ;
61 };
62 } // namespace DeferredProcessing
63 } // namespace CameraStandard
64 } // namespace OHOS
65 #endif // OHOS_CAMERA_DPS_VIDEO_JOB_REPOSITORY_H