• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H
17 #define FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include <securec.h>
22 #include <string>
23 #include <surface.h>
24 
25 #include "image_format.h"
26 #include "image_type.h"
27 #include "media_errors.h"
28 #include "pixel_map.h"
29 #include "image_receiver_context.h"
30 #include "image/image_receiver_native.h"
31 #include "native_image.h"
32 #include "surface_utils.h"
33 
34 namespace OHOS {
35 namespace Media {
36 class IBufferProcessor;
37 class NativeImage;
38 class SurfaceBufferAvaliableListener {
39 public:
40     SurfaceBufferAvaliableListener()= default;
41     virtual ~SurfaceBufferAvaliableListener()= default;
42     virtual void OnSurfaceBufferAvaliable() = 0;
43 };
44 
45 class ImageReceiverArriveListener : public SurfaceBufferAvaliableListener {
46 public:
ImageReceiverArriveListener(OH_ImageReceiverNative * receiver)47     explicit ImageReceiverArriveListener(OH_ImageReceiverNative* receiver) : receiver_(receiver) {}
48 
~ImageReceiverArriveListener()49     ~ImageReceiverArriveListener() override
50     {
51         std::lock_guard<std::mutex> lock(mutex_);
52         callbacks_.clear();
53     }
54 
HasCallback(OH_ImageReceiver_ImageArriveCallback callback)55     bool HasCallback(OH_ImageReceiver_ImageArriveCallback callback) {
56         std::lock_guard<std::mutex> lock(mutex_);
57         return std::any_of(callbacks_.begin(), callbacks_.end(),
58                            [callback](const CallbackData &cbData) { return cbData.callback == callback; });
59     }
60 
RegisterCallback(OH_ImageReceiver_ImageArriveCallback callback,void * userdata)61     bool RegisterCallback(OH_ImageReceiver_ImageArriveCallback callback, void* userdata)
62     {
63         if (callback == nullptr) {
64             return false;
65         }
66         if (HasCallback(callback)) {
67             return false;
68         }
69         std::lock_guard<std::mutex> lock(mutex_);
70         callbacks_.emplace_back(callback, userdata);
71         return true;
72     }
73 
UnregisterCallback(OH_ImageReceiver_ImageArriveCallback callback)74     bool UnregisterCallback(OH_ImageReceiver_ImageArriveCallback callback)
75     {
76         if (callback == nullptr) {
77             return false;
78         }
79         std::lock_guard<std::mutex> lock(mutex_);
80         auto it = std::remove_if(callbacks_.begin(), callbacks_.end(),
81             [callback](const CallbackData &cbData) { return cbData.callback == callback; });
82         if (it != callbacks_.end()) {
83             callbacks_.erase(it, callbacks_.end());
84             return true;
85         }
86         return false;
87     }
88 
OnSurfaceBufferAvaliable()89     void OnSurfaceBufferAvaliable() __attribute__((no_sanitize("cfi"))) override
90     {
91         std::lock_guard<std::mutex> lock(mutex_);
92         for (const auto& cbData : callbacks_) {
93             if (cbData.callback != nullptr) {
94                 cbData.callback(receiver_, cbData.data);
95             }
96         }
97     }
98 
99 private:
100     struct CallbackData {
101         OH_ImageReceiver_ImageArriveCallback callback;
102         void* data;
103 
CallbackDataCallbackData104         CallbackData(OH_ImageReceiver_ImageArriveCallback cb, void* d) : callback(cb), data(d) {}
105     };
106 
107     OH_ImageReceiverNative* receiver_ = nullptr;
108     std::vector<CallbackData> callbacks_;
109     mutable std::mutex mutex_;
110 };
111 
112 class ImageReceiver {
113 public:
114     std::shared_ptr<ImageReceiverContext> iraContext_ = nullptr;
115     sptr<IConsumerSurface> receiverConsumerSurface_ = nullptr;
116     sptr<Surface> receiverProducerSurface_ = nullptr;
117     std::mutex imageReceiverMutex_;
118     std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener_ = nullptr;
119     std::shared_ptr<ImageReceiverArriveListener> surfaceBufferAvaliableArriveListener_ = nullptr;
ImageReceiver()120     ImageReceiver() {}
121     ~ImageReceiver();
122     static inline int32_t pipeFd[2] = {};
123     static inline std::string OPTION_FORMAT = "image/jpeg";
124     static inline std::int32_t OPTION_QUALITY = 100;
125     static inline std::int32_t OPTION_NUMBERHINT = 1;
126     static std::shared_ptr<ImageReceiver> CreateImageReceiver(int32_t width,
127                                                               int32_t height,
128                                                               int32_t format,
129                                                               int32_t capicity);
130     sptr<Surface> GetReceiverSurface();
131     OHOS::sptr<OHOS::SurfaceBuffer> ReadNextImage();
132     OHOS::sptr<OHOS::SurfaceBuffer> ReadLastImage();
133     OHOS::sptr<OHOS::SurfaceBuffer> ReadNextImage(int64_t &timestamp);
134     OHOS::sptr<OHOS::SurfaceBuffer> ReadLastImage(int64_t &timestamp);
135     int32_t SaveBufferAsImage(int &fd,
136                               OHOS::sptr<OHOS::SurfaceBuffer> buffer,
137                               InitializationOptions initializationOpts);
138     int32_t SaveBufferAsImage(int &fd, InitializationOptions initializationOpts);
139     void ReleaseBuffer(OHOS::sptr<OHOS::SurfaceBuffer> &buffer);
140     std::unique_ptr<PixelMap> getSurfacePixelMap(InitializationOptions initializationOpts);
RegisterBufferAvaliableListener(std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener)141     void RegisterBufferAvaliableListener(
142         std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener)
143     {
144         surfaceBufferAvaliableListener_ = surfaceBufferAvaliableListener;
145     }
UnRegisterBufferAvaliableListener()146     void UnRegisterBufferAvaliableListener()
147     {
148         surfaceBufferAvaliableListener_.reset();
149     }
150     static sptr<Surface> getSurfaceById(std::string id);
151     void ReleaseReceiver();
152 
153     std::shared_ptr<IBufferProcessor> GetBufferProcessor();
154     std::shared_ptr<NativeImage> NextNativeImage();
155     std::shared_ptr<NativeImage> LastNativeImage();
156 private:
157     std::shared_ptr<IBufferProcessor> bufferProcessor_;
158 };
159 class ImageReceiverSurfaceListener : public IBufferConsumerListener {
160 public:
161     std::weak_ptr<ImageReceiver> ir_;
162     void OnBufferAvailable() override;
163 };
164 } // namespace Media
165 } // namespace OHOS
166 
167 #endif // FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H
168