1 /*
2  * Copyright (c) 2021-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_ICAMERA_SOURCE_DATA_PROCESS_PRODUCER_H
17 #define OHOS_ICAMERA_SOURCE_DATA_PROCESS_PRODUCER_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <queue>
22 #include <deque>
23 #include <thread>
24 
25 #include "data_buffer.h"
26 #include "event_handler.h"
27 #include "v1_1/id_camera_provider.h"
28 #include "dcamera_feeding_smoother.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 using namespace OHOS::HDI::DistributedCamera::V1_1;
33 class DCameraStreamDataProcessProducer : public IFeedableDataProducer,
34     public std::enable_shared_from_this<DCameraStreamDataProcessProducer> {
35 public:
36     typedef enum {
37         DCAMERA_PRODUCER_STATE_STOP = 0,
38         DCAMERA_PRODUCER_STATE_START = 1,
39     } DCameraProducerState;
40 
41     DCameraStreamDataProcessProducer(std::string devId, std::string dhId, int32_t streamId, DCStreamType streamType);
42     ~DCameraStreamDataProcessProducer();
43     void Start();
44     void Stop();
45     void FeedStream(const std::shared_ptr<DataBuffer>& buffer);
46     virtual void OnSmoothFinished(const std::shared_ptr<IFeedableData>& data) override;
47 
48 private:
49     void StartEvent();
50     void LooperSnapShot();
51     int32_t FeedStreamToDriver(const DHBase& dhBase, const std::shared_ptr<DataBuffer>& buffer);
52     int32_t CheckSharedMemory(const DCameraBuffer& sharedMemory, const std::shared_ptr<DataBuffer>& buffer);
53 
54     const uint32_t DCAMERA_PRODUCER_MAX_BUFFER_SIZE = 30;
55     const uint32_t DCAMERA_PRODUCER_RETRY_SLEEP_MS = 500;
56 
57 private:
58     std::string devId_;
59     std::string dhId_;
60 
61     std::thread eventThread_;
62     std::thread producerThread_;
63     std::condition_variable eventCon_;
64     std::condition_variable producerCon_;
65     std::mutex bufferMutex_;
66     std::mutex eventMutex_;
67     std::deque<std::shared_ptr<DataBuffer>> buffers_;
68     DCameraProducerState state_;
69     uint32_t interval_;
70     uint32_t photoCount_;
71     int32_t streamId_;
72     DCStreamType streamType_;
73     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
74 
75     sptr<IDCameraProvider> camHdiProvider_;
76     std::unique_ptr<IFeedingSmoother> smoother_ = nullptr;
77     std::shared_ptr<FeedingSmootherListener> smootherListener_ = nullptr;
78 };
79 } // namespace DistributedHardware
80 } // namespace OHOS
81 #endif // OHOS_ICAMERA_SOURCE_DATA_PROCESS_PRODUCER_H
82