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 "display_type.h"
30 #include "image_receiver_context.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 class ImageReceiver {
45 public:
46     std::shared_ptr<ImageReceiverContext> iraContext_ = nullptr;
47     sptr<IConsumerSurface> receiverConsumerSurface_ = nullptr;
48     sptr<Surface> receiverProducerSurface_ = nullptr;
49     std::mutex imageReceiverMutex_;
50     std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener_ = nullptr;
ImageReceiver()51     ImageReceiver() {}
52     ~ImageReceiver();
53     static inline int32_t pipeFd[2] = {};
54     static inline std::string OPTION_FORMAT = "image/jpeg";
55     static inline std::int32_t OPTION_QUALITY = 100;
56     static inline std::int32_t OPTION_NUMBERHINT = 1;
57     static std::shared_ptr<ImageReceiver> CreateImageReceiver(int32_t width,
58                                                               int32_t height,
59                                                               int32_t format,
60                                                               int32_t capicity);
61     sptr<Surface> GetReceiverSurface();
62     OHOS::sptr<OHOS::SurfaceBuffer> ReadNextImage();
63     OHOS::sptr<OHOS::SurfaceBuffer> ReadLastImage();
64     OHOS::sptr<OHOS::SurfaceBuffer> ReadNextImage(int64_t &timestamp);
65     OHOS::sptr<OHOS::SurfaceBuffer> ReadLastImage(int64_t &timestamp);
66     int32_t SaveBufferAsImage(int &fd,
67                               OHOS::sptr<OHOS::SurfaceBuffer> buffer,
68                               InitializationOptions initializationOpts);
69     int32_t SaveBufferAsImage(int &fd, InitializationOptions initializationOpts);
70     void ReleaseBuffer(OHOS::sptr<OHOS::SurfaceBuffer> &buffer);
71     std::unique_ptr<PixelMap> getSurfacePixelMap(InitializationOptions initializationOpts);
RegisterBufferAvaliableListener(std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener)72     void RegisterBufferAvaliableListener(
73         std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener)
74     {
75         surfaceBufferAvaliableListener_ = surfaceBufferAvaliableListener;
76     }
UnRegisterBufferAvaliableListener()77     void UnRegisterBufferAvaliableListener()
78     {
79         surfaceBufferAvaliableListener_.reset();
80     }
81     static sptr<Surface> getSurfaceById(std::string id);
82     void ReleaseReceiver();
83 
84     std::shared_ptr<IBufferProcessor> GetBufferProcessor();
85     std::shared_ptr<NativeImage> NextNativeImage();
86     std::shared_ptr<NativeImage> LastNativeImage();
87 private:
88     std::shared_ptr<IBufferProcessor> bufferProcessor_;
89 };
90 class ImageReceiverSurfaceListener : public IBufferConsumerListener {
91 public:
92     std::weak_ptr<ImageReceiver> ir_;
93     void OnBufferAvailable() override;
94 };
95 } // namespace Media
96 } // namespace OHOS
97 
98 #endif // FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H
99