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 "hilog/log.h" 26 #include "image_format.h" 27 #include "image_type.h" 28 #include "log_tags.h" 29 #include "media_errors.h" 30 #include "pixel_map.h" 31 #include "display_type.h" 32 #include "image_receiver_context.h" 33 #include "native_image.h" 34 35 namespace OHOS { 36 namespace Media { 37 class IBufferProcessor; 38 class NativeImage; 39 class SurfaceBufferAvaliableListener { 40 public: 41 SurfaceBufferAvaliableListener()= default; 42 virtual ~SurfaceBufferAvaliableListener()= default; 43 virtual void OnSurfaceBufferAvaliable() = 0; 44 }; 45 class ImageReceiver { 46 public: 47 std::shared_ptr<ImageReceiverContext> iraContext_ = nullptr; 48 sptr<IConsumerSurface> receiverConsumerSurface_ = nullptr; 49 sptr<Surface> receiverProducerSurface_ = nullptr; 50 std::mutex imageReceiverMutex_; 51 std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener_ = nullptr; ImageReceiver()52 ImageReceiver() {} 53 ~ImageReceiver(); 54 static inline int32_t pipeFd[2] = {}; 55 static inline std::string OPTION_FORMAT = "image/jpeg"; 56 static inline std::int32_t OPTION_QUALITY = 100; 57 static inline std::int32_t OPTION_NUMBERHINT = 1; 58 static std::shared_ptr<ImageReceiver> CreateImageReceiver(int32_t width, 59 int32_t height, 60 int32_t format, 61 int32_t capicity); 62 sptr<Surface> GetReceiverSurface(); 63 OHOS::sptr<OHOS::SurfaceBuffer> ReadNextImage(); 64 OHOS::sptr<OHOS::SurfaceBuffer> ReadLastImage(); 65 int32_t SaveBufferAsImage(int &fd, 66 OHOS::sptr<OHOS::SurfaceBuffer> buffer, 67 InitializationOptions initializationOpts); 68 int32_t SaveBufferAsImage(int &fd, InitializationOptions initializationOpts); 69 void ReleaseBuffer(OHOS::sptr<OHOS::SurfaceBuffer> &buffer); 70 std::unique_ptr<PixelMap> getSurfacePixelMap(InitializationOptions initializationOpts); RegisterBufferAvaliableListener(std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener)71 void RegisterBufferAvaliableListener( 72 std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener) 73 { 74 surfaceBufferAvaliableListener_ = surfaceBufferAvaliableListener; 75 } 76 static sptr<Surface> getSurfaceById(std::string id); 77 void ReleaseReceiver(); 78 79 std::shared_ptr<IBufferProcessor> GetBufferProcessor(); 80 std::shared_ptr<NativeImage> NextNativeImage(); 81 std::shared_ptr<NativeImage> LastNativeImage(); 82 private: 83 std::shared_ptr<IBufferProcessor> bufferProcessor_; 84 }; 85 class ImageReceiverSurfaceListener : public IBufferConsumerListener { 86 public: 87 std::shared_ptr<ImageReceiver> ir_; 88 void OnBufferAvailable() override; 89 }; 90 } // namespace Media 91 } // namespace OHOS 92 93 #endif // FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H_ 94