1 /*
2  * Copyright (c) 2021 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 HOS_CAMERA_OFFLINE_PIPELINE_H
17 #define HOS_CAMERA_OFFLINE_PIPELINE_H
18 
19 #include "camera.h"
20 #include "ibuffer.h"
21 #include <atomic>
22 #include <condition_variable>
23 #include <functional>
24 #include <list>
25 #include <memory>
26 #include <mutex>
27 #include <thread>
28 
29 namespace OHOS::Camera {
30 class OfflinePipeline {
31 public:
32     OfflinePipeline();
33     virtual ~OfflinePipeline();
34     void DeliverCacheCheck(std::vector<std::shared_ptr<IBuffer>>& buffers);
35     RetCode StartProcess();
36     RetCode StopProcess();
37     void BindOfflineStreamCallback(std::function<void(std::shared_ptr<IBuffer>&)>& callback);
38     void SwitchToOfflineMode();
39     void ReceiveCache(std::vector<std::shared_ptr<IBuffer>>& buffers);
40     RetCode CancelCapture(int32_t captureId);
41     RetCode FlushOfflineStream();
42     bool CacheQueueDry();
43     bool CheckOwnerOfCaptureId(int32_t captureId);
44 
45     virtual void DeliverOfflineBuffer(std::shared_ptr<IBuffer>& buffer);
46     virtual void ProcessCache(std::vector<std::shared_ptr<IBuffer>>& buffers);
47     virtual void DeliverCache(std::vector<std::shared_ptr<IBuffer>>& buffers);
48     virtual void DeliverCancelCache(std::vector<std::shared_ptr<IBuffer>>& buffers);
49 
50 public:
51     std::atomic<bool> offlineMode_ = false;
52     std::list<std::vector<std::shared_ptr<IBuffer>>> bufferCache_ = {};
53     std::function<void(std::shared_ptr<IBuffer>&)> callback_ = nullptr;
54 
55 private:
56     void HandleBuffers();
57 
58 private:
59     std::mutex cbLock_;
60     std::mutex queueLock_;
61     std::condition_variable cv_;
62     std::atomic<bool> running_ = false;
63     std::thread* processThread_ = nullptr;
64     std::atomic<int> calltimes_;
65 };
66 } // namespace OHOS::Camera
67 #endif
68