1 /* 2 * Copyright (c) 2024 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 VIDEO_PROCESSING_NATIVE_BASE_H 17 #define VIDEO_PROCESSING_NATIVE_BASE_H 18 19 #include <atomic> 20 #include <functional> 21 #include <memory> 22 #include <string> 23 #include <mutex> 24 #include <set> 25 26 #include "common/native_mfmagic.h" 27 #include "surface.h" 28 29 #include "video_processing_callback_native.h" 30 #include "video_processing_interface.h" 31 32 namespace OHOS { 33 namespace Media { 34 namespace VideoProcessingEngine { 35 /** 36 * Base implementaion for video processing. 37 */ 38 class VideoProcessingNativeBase : public IVideoProcessingNative { 39 public: 40 VideoProcessing_ErrorCode Initialize() final; 41 VideoProcessing_ErrorCode Deinitialize() final; 42 VideoProcessing_ErrorCode RegisterCallback(const VideoProcessing_Callback* callback, void* userData) final; 43 VideoProcessing_ErrorCode SetSurface(const OHNativeWindow* window) final; 44 VideoProcessing_ErrorCode GetSurface(OHNativeWindow** window) final; 45 VideoProcessing_ErrorCode SetParameter(const OH_AVFormat* parameter) final; 46 VideoProcessing_ErrorCode GetParameter(OH_AVFormat* parameter) final; 47 VideoProcessing_ErrorCode Start() final; 48 VideoProcessing_ErrorCode Stop() final; 49 VideoProcessing_ErrorCode RenderOutputBuffer(uint32_t index) final; 50 51 protected: 52 explicit VideoProcessingNativeBase(OH_VideoProcessing* context); 53 virtual ~VideoProcessingNativeBase() = default; 54 VideoProcessingNativeBase(const VideoProcessingNativeBase&) = delete; 55 VideoProcessingNativeBase& operator=(const VideoProcessingNativeBase&) = delete; 56 VideoProcessingNativeBase(VideoProcessingNativeBase&&) = delete; 57 VideoProcessingNativeBase& operator=(VideoProcessingNativeBase&&) = delete; 58 59 virtual VideoProcessing_ErrorCode InitializeInner(); 60 virtual VideoProcessing_ErrorCode DeinitializeInner(); 61 virtual VideoProcessing_ErrorCode RegisterCallback(); 62 virtual VideoProcessing_ErrorCode SetSurface(const sptr<Surface>& surface, const OHNativeWindow& window); 63 virtual VideoProcessing_ErrorCode SetSurface(const sptr<Surface>& surface); 64 virtual sptr<Surface> GetSurface(); 65 virtual VideoProcessing_ErrorCode SetParameter(const OHOS::Media::Format& parameter); 66 virtual VideoProcessing_ErrorCode GetParameter(OHOS::Media::Format& parameter); 67 virtual VideoProcessing_ErrorCode OnStart(); 68 virtual VideoProcessing_ErrorCode OnStop(); 69 virtual VideoProcessing_ErrorCode OnRenderOutputBuffer(uint32_t index); 70 71 // Called by child classes of features(Such as DetailEnhancerVideoNative) to send information to users' callbacks 72 void OnError(VideoProcessing_ErrorCode errorCode); 73 void OnState(VideoProcessing_State state); 74 void OnNewOutputBuffer(uint32_t index); 75 76 private: 77 struct CallbackInfo { 78 std::shared_ptr<VideoProcessingCallbackNative> callback{}; 79 void* userData{}; 80 bool operator<(const CallbackInfo& other) const 81 { 82 return (callback == other.callback) ? (userData < other.userData) : (callback < other.callback); 83 } 84 }; 85 86 VideoProcessing_ErrorCode ExecuteWhenIdle(std::function<VideoProcessing_ErrorCode(void)>&& task, 87 const std::string& errLog); 88 VideoProcessing_ErrorCode ExecuteWhenRunning(std::function<VideoProcessing_ErrorCode(void)>&& task, 89 const std::string& errLog); 90 void OnCallback(std::function<void(std::shared_ptr<VideoProcessingCallbackNative>&, void*)>&& task, 91 const std::string& name); 92 void TraverseCallbacksLocked(std::function<void(std::shared_ptr<VideoProcessingCallbackNative>&, void*)>&& task); 93 void TraverseCallbacksExLocked(std::function<bool(std::shared_ptr<VideoProcessingCallbackNative>&, void*)>&& task); 94 VideoProcessing_ErrorCode PrepareRegistrationLocked(); 95 96 OH_VideoProcessing* context_{}; 97 mutable std::mutex lock_{}; 98 // Guarded by lock_ begin 99 std::atomic<bool> isInitialized_{false}; 100 std::atomic<bool> isRunning_{false}; 101 std::atomic<bool> hasInputSurface_{false}; 102 std::atomic<bool> hasOutputSurface_{false}; 103 // Guarded by lock_ end 104 std::atomic<bool> isOnNewOutputBuffer_{}; 105 mutable std::mutex callbackLock_{}; 106 // Guarded by callbackLock_ begin 107 std::atomic<bool> hasCallback_{false}; 108 std::atomic<bool> hasOnRenderOutputBuffer_{false}; 109 bool isInnerCallbackReady_{false}; 110 std::set<CallbackInfo> callbacks_{}; 111 // Guarded by callbackLock_ end 112 }; 113 } // namespace VideoProcessingEngine 114 } // namespace Media 115 } // namespace OHOS 116 117 #endif // VIDEO_PROCESSING_NATIVE_BASE_H 118