/* * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H #define FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H #include #include #include #include #include #include "image_format.h" #include "image_type.h" #include "media_errors.h" #include "pixel_map.h" #include "image_receiver_context.h" #include "image/image_receiver_native.h" #include "native_image.h" #include "surface_utils.h" namespace OHOS { namespace Media { class IBufferProcessor; class NativeImage; class SurfaceBufferAvaliableListener { public: SurfaceBufferAvaliableListener()= default; virtual ~SurfaceBufferAvaliableListener()= default; virtual void OnSurfaceBufferAvaliable() = 0; }; class ImageReceiverArriveListener : public SurfaceBufferAvaliableListener { public: explicit ImageReceiverArriveListener(OH_ImageReceiverNative* receiver) : receiver_(receiver) {} ~ImageReceiverArriveListener() override { std::lock_guard lock(mutex_); callbacks_.clear(); } bool HasCallback(OH_ImageReceiver_ImageArriveCallback callback) { std::lock_guard lock(mutex_); return std::any_of(callbacks_.begin(), callbacks_.end(), [callback](const CallbackData &cbData) { return cbData.callback == callback; }); } bool RegisterCallback(OH_ImageReceiver_ImageArriveCallback callback, void* userdata) { if (callback == nullptr) { return false; } if (HasCallback(callback)) { return false; } std::lock_guard lock(mutex_); callbacks_.emplace_back(callback, userdata); return true; } bool UnregisterCallback(OH_ImageReceiver_ImageArriveCallback callback) { if (callback == nullptr) { return false; } std::lock_guard lock(mutex_); auto it = std::remove_if(callbacks_.begin(), callbacks_.end(), [callback](const CallbackData &cbData) { return cbData.callback == callback; }); if (it != callbacks_.end()) { callbacks_.erase(it, callbacks_.end()); return true; } return false; } void OnSurfaceBufferAvaliable() __attribute__((no_sanitize("cfi"))) override { std::lock_guard lock(mutex_); for (const auto& cbData : callbacks_) { if (cbData.callback != nullptr) { cbData.callback(receiver_, cbData.data); } } } private: struct CallbackData { OH_ImageReceiver_ImageArriveCallback callback; void* data; CallbackData(OH_ImageReceiver_ImageArriveCallback cb, void* d) : callback(cb), data(d) {} }; OH_ImageReceiverNative* receiver_ = nullptr; std::vector callbacks_; mutable std::mutex mutex_; }; class ImageReceiver { public: std::shared_ptr iraContext_ = nullptr; sptr receiverConsumerSurface_ = nullptr; sptr receiverProducerSurface_ = nullptr; std::mutex imageReceiverMutex_; std::shared_ptr surfaceBufferAvaliableListener_ = nullptr; std::shared_ptr surfaceBufferAvaliableArriveListener_ = nullptr; ImageReceiver() {} ~ImageReceiver(); static inline int32_t pipeFd[2] = {}; static inline std::string OPTION_FORMAT = "image/jpeg"; static inline std::int32_t OPTION_QUALITY = 100; static inline std::int32_t OPTION_NUMBERHINT = 1; static std::shared_ptr CreateImageReceiver(int32_t width, int32_t height, int32_t format, int32_t capicity); sptr GetReceiverSurface(); OHOS::sptr ReadNextImage(); OHOS::sptr ReadLastImage(); OHOS::sptr ReadNextImage(int64_t ×tamp); OHOS::sptr ReadLastImage(int64_t ×tamp); int32_t SaveBufferAsImage(int &fd, OHOS::sptr buffer, InitializationOptions initializationOpts); int32_t SaveBufferAsImage(int &fd, InitializationOptions initializationOpts); void ReleaseBuffer(OHOS::sptr &buffer); std::unique_ptr getSurfacePixelMap(InitializationOptions initializationOpts); void RegisterBufferAvaliableListener( std::shared_ptr surfaceBufferAvaliableListener) { surfaceBufferAvaliableListener_ = surfaceBufferAvaliableListener; } void UnRegisterBufferAvaliableListener() { surfaceBufferAvaliableListener_.reset(); } static sptr getSurfaceById(std::string id); void ReleaseReceiver(); std::shared_ptr GetBufferProcessor(); std::shared_ptr NextNativeImage(); std::shared_ptr LastNativeImage(); private: std::shared_ptr bufferProcessor_; }; class ImageReceiverSurfaceListener : public IBufferConsumerListener { public: std::weak_ptr ir_; void OnBufferAvailable() override; }; } // namespace Media } // namespace OHOS #endif // FRAMEWORKS_INNERKITSIMPL_RECEIVER_INCLUDE_IMAGE_RECEIVER_H