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 <surface.h> 20 #include <cstdint> 21 #include <string> 22 #include <securec.h> 23 #include "hilog/log.h" 24 #include "image_format.h" 25 #include "image_type.h" 26 #include "log_tags.h" 27 #include "media_errors.h" 28 #include "pixel_map.h" 29 #include "display_type.h" 30 #include "image_receiver_context.h" 31 32 namespace OHOS { 33 namespace Media { 34 class SurfaceBufferAvaliableListener { 35 public: 36 SurfaceBufferAvaliableListener()= default; 37 virtual ~SurfaceBufferAvaliableListener()= default; 38 virtual void OnSurfaceBufferAvaliable() = 0; 39 }; 40 class ImageReceiver { 41 public: 42 std::shared_ptr<ImageReceiverContext> iraContext_ = nullptr; 43 sptr<Surface> receiverConsumerSurface_ = nullptr; 44 sptr<Surface> receiverProducerSurface_ = nullptr; 45 std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener_ = nullptr; ImageReceiver()46 ImageReceiver() {} ~ImageReceiver()47 ~ImageReceiver() 48 { 49 receiverConsumerSurface_ = nullptr; 50 receiverProducerSurface_ = nullptr; 51 iraContext_ = nullptr; 52 surfaceBufferAvaliableListener_ = nullptr; 53 } 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 class ImageReceiverSurfaceListener : public IBufferConsumerListener { 80 public: 81 std::shared_ptr<ImageReceiver> ir_; 82 void OnBufferAvailable() override; 83 }; 84 } // namespace Media 85 } // namespace OHOS 86 87 #endif // FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H_ 88