• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 STREAM_OPERATOR_CAPTURE_REQUEST_H
17 #define STREAM_OPERATOR_CAPTURE_REQUEST_H
18 
19 #include "camera_metadata_info.h"
20 #include "istream.h"
21 #include "stream.h"
22 #include <atomic>
23 #include <condition_variable>
24 #include <map>
25 #include <memory>
26 
27 namespace OHOS::Camera {
28 class IStream;
29 using CaptureSetting = std::weak_ptr<CameraMetadata>;
30 
31 class CaptureRequest final : public std::enable_shared_from_this<CaptureRequest> {
32 public:
33     CaptureRequest(const int32_t id, const int32_t n, CaptureMeta& setting, bool needReport, bool isContinuous);
34     ~CaptureRequest();
35     RetCode AddOwner(const std::weak_ptr<IStream>& owner);
36     RetCode Process(const int32_t id);
37     RetCode OnResult(const int32_t id);
38     RetCode Cancel();
39     uint32_t GetOwnerCount();
40     void DisableSync();
41     uint64_t GetBeginTime() const;
42     uint64_t GetEndTime() const;
43     bool NeedShutterCallback() const;
44     bool IsContinous() const;
45     int32_t GetCaptureId() const;
46     void AttachBuffer(std::shared_ptr<IBuffer>& b);
47     std::shared_ptr<IBuffer> GetAttachedBuffer() const;
48     bool NeedCancel() const;
49     void SetFirstRequest(bool b);
50     bool IsFirstOne() const;
51     CaptureMeta GetCaptureSetting() const;
52 
53 private:
54     class RequestSemaphore final {
55     public:
56         RequestSemaphore() = default;
57         RequestSemaphore(const int32_t n);
58         ~RequestSemaphore();
59         void Sync();
60 
61     private:
62         uint64_t GenerateTimeStamp();
63 
64     public:
65         std::mutex sem_ = {};
66         uint64_t timestamp_ = 0;
67         std::atomic<uint32_t> ownerCount_ = 0;
68         std::atomic<uint32_t> syncCount_ = 0;
69         std::condition_variable cv_ = {};
70         std::atomic<bool> needSync = true;
71     };
72 
73     int32_t captureId_ = -1;
74     std::mutex lock_ = {};
75     std::map<int32_t, std::weak_ptr<IStream>> owners_ = {};
76     std::unique_ptr<RequestSemaphore> semp_ = nullptr;
77     std::unique_ptr<RequestSemaphore> semr_ = nullptr;
78     CaptureMeta settings_;
79     bool needShutterCallback_ = false;
80     bool isContinuous_ = false;
81     std::shared_ptr<IBuffer> buffer_ = nullptr;
82     std::atomic<bool> needCancel_ = false;
83     uint32_t ownerCount_ = 0;
84     bool isFirstRequest_ = false;
85 };
86 } // namespace OHOS::Camera
87 #endif
88